Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django表单问题(1视图中的列表和表单)_Python_Django_Django Forms - Fatal编程技术网

Python django表单问题(1视图中的列表和表单)

Python django表单问题(1视图中的列表和表单),python,django,django-forms,Python,Django,Django Forms,我的问题是在提交表格之后。它看起来像这样,提交表单,然后在提交后重新加载页面,我可以在列表中看到标题电影,但是在表单下它说“这个标题在数据库中” 如有任何提示,我将不胜感激:) class MovieListWithForm(ListView, ModelFormMixin): model = Movie form_class = MovieForm template_name = 'pages/movie_list.html' def get(self, re

我的问题是在提交表格之后。它看起来像这样,提交表单,然后在提交后重新加载页面,我可以在列表中看到标题电影,但是在表单下它说“这个标题在数据库中”

如有任何提示,我将不胜感激:)

class MovieListWithForm(ListView, ModelFormMixin):
    model = Movie
    form_class = MovieForm
    template_name = 'pages/movie_list.html'

    def get(self, request, *args, **kwargs):
        self.object = None
        self.form = self.get_form(self.form_class)
        return ListView.get(self, request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        # When the form is submitted, it will enter here
        self.object = None
        self.form = self.get_form(self.form_class)

        if self.form.is_valid():
            self.object = self.form.save()
            self.form = MovieForm()

        return self.get(request, *args, **kwargs)

    def get_context_data(self, *args, **kwargs):
        # Just include the form
        context = super(MovieListWithForm, self).get_context_data(*args, **kwargs)
        context['form'] = self.form
        context['list'] = Movie.objects.all().order_by('votes')
        return context


class MovieForm(ModelForm):
 class Meta:
    model = Movie
    fields = ['title']

 def __init__(self, *args, **kwargs):
    super(MovieForm, self).__init__(*args, **kwargs)
    self.fields['title'].label = "Movie title"

 def clean_title(self):
    data = self.cleaned_data['title']
    if Movie.objects.filter(title=data).exists():
        raise ValidationError("this title is in base")
    if data == None:
        raise ValidationError("Add title")

    return data