我在 models.py 中定义了一个类别的选择,它在 forms.py 中被引用,如下所示:
模型.py
class Exp(models.Model):
CATEGORIES = ( ('Inc', 'Inc'),\
('Hom', 'Hom'),\
('syn', 'Syn'),\
('Was', 'Was')\
)
表格.py
class ExpForm(ModelForm):
category = forms.MultipleChoiceField(choices=Exp.CATEGORIES, widget=forms.CheckboxSelectMultiple())
class Meta:
model = Exp
视图.py
def view_exp(request):
if request.method == "POST":
form = ExpForm(request.POST)
if form.is_valid():
...
多选选项在表单中正确显示,但是当我选择多个选项时,form.is_valid()
inviews.py
返回 false 并出现AttributeError: 'str' object has no attribute 'status_code
错误。我知道我得到的是 astr
而不是一个HttpResponse
对象,但不知道如何解决这个错误。任何帮助表示赞赏。
注意:同一表单中还有其他类型为CharField
.
这是回溯:
Traceback (most recent call last):
File "/software/python/python-2.7.3/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in get_response
response = middleware_method(request, response)
File "/software/python/python-2.7.3/lib/python2.7/site-packages/django/middleware/common.py", line 106, in process_response
if response.status_code == 404:
AttributeError: 'str' object has no attribute 'status_code'