尝试在我的主页视图中添加表单,并在提交表单时收到此错误。这是我的代码:
views.py
class BoxesView(ListView, FormMixin):
template_name = 'polls.html'
form_class = UserRegistrationForm
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = User.objects.create_user(username=username, password=password)
user.save()
return redirect('/')
return self.form_invalid(form)
def get_context_data(self, **kwargs):
context = super(BoxesView, self).get_context_data()
context['form'] = self.get_form()
question_list = Question.objects.all().order_by('-date')
choice = Choice.objects.all()
context['question_list'] = question_list
context['choice'] = choice
q_list = []
returned_list = []
for i in question_list:
q_list.append(i)
for a, b in CATEGORY_CHOICES:
name = resolve(self.request.path_info).url_name
if b == name:
category = a
search = self.request.GET.get('search')
posts = Post.objects.all().filter(category=category).order_by('-date')
if search:
posts = posts.filter(
Q(title__icontains=search) |
Q(content__icontains=search)
)
else:
posts = Post.objects.all().filter(category=category).order_by('-date')
context['posts'] = posts
total = 0
for post in posts:
returned_list.append(post)
total += 1
if total == 4:
total = 0
for i in q_list:
returned_list.append(i)
q_list.remove(i)
break
search = self.request.GET.get('search')
posts = Post.objects.all().filter(category=category).order_by('-date')
if search:
posts = posts.filter(
Q(title__icontains=search) |
Q(content__icontains=search)
)
else:
posts = Post.objects.all().filter(category=category).order_by('-date')
context['posts'] = posts
return context
def get_queryset(self):
pass
forms.py
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = [
'username',
'password',
]
base.html文件
<form action="" enctype="multipart/form-data" method="post">{% csrf_token %}
<div class="registerBox">
{{ form.username }}
{{ form.password }}
<input type="submit" value="register"/>
</div>
</form>
我的追溯指向了以下几行:return self.form_invalid(form)
&amp; context = super(BoxesView, self).get_context_data()
。希望这可以告诉你问题是什么,但我一直无法解决。任何想法?
答案 0 :(得分:2)
如果您使用的是ListView,则应specify a model attribute
使用
class BoxesView(ListView, FormMixin):
template_name = 'polls.html'
model = yourModel
或者您应该在get_queryset方法中指定queryset。
def get_queryset(self):
qs = yourModel.objects.all()
return qs
请注意queryset is accessed in templates by the default name object_list
。
另外,不是使用ListView中的表单创建对象,而是只尝试在ListView中列出对象。 Use CreateView to create objects
。您的ListView可能如下所示。
class BoxesView(ListView):
template_name = 'polls.html'
model = yourModel
paginate_by = 20
有关详细信息,请参阅CORS (Cross-Origin Resource Sharing)
同样从您的问题看来,您似乎正在尝试迭代Post对象,这些对象作为上下文数据传递给模板。请注意,Overriding def get_context_data(self) is to provide extra context variables
,而不是queryset和ListView总是期望在从ListView继承的类中定义model或get_queryset(self)。如果您要做的是为Post对象创建ListView,请尝试在班级中设置model=Post
。
答案 1 :(得分:1)
我管理一个解决方案,如果您的类中没有user_list
变量,则必须将其放入,但最好重写构造函数,但仍需将model变量放入您的类中。
class BoxesView(ListView, FormMixin):
template_name = 'polls.html'
form_class = UserRegistrationForm
model = YourModel
def __init__(self, *args, **kwargs):
super(BoxesView, self).__init__(*args, **kwargs)
self.object_list = self.get_queryset()
这很有用:P