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