我的一个表单给了我这个错误,我似乎无法弄清楚问题是什么。
11:24:04 web.1 | Internal Server Error: /dash/location/add
11:24:04 web.1 | Traceback (most recent call last):
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
11:24:04 web.1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
11:24:04 web.1 | return view_func(request, *args, **kwargs)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 69, in view
11:24:04 web.1 | return self.dispatch(request, *args, **kwargs)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
11:24:04 web.1 | return handler(request, *args, **kwargs)
11:24:04 web.1 | File "/Users/nir/dashboard/pinpoint/apps/locationmanager/views.py", line 43, in post
11:24:04 web.1 | if form.is_valid():
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 129, in is_valid
11:24:04 web.1 | return self.is_bound and not bool(self.errors)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 121, in errors
11:24:04 web.1 | self.full_clean()
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 273, in full_clean
11:24:04 web.1 | self._clean_fields()
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 288, in _clean_fields
11:24:04 web.1 | value = field.clean(value)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/fields.py", line 148, in clean
11:24:04 web.1 | value = self.to_python(value)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/models.py", line 1136, in to_python
11:24:04 web.1 | except (ValueError, self.queryset.model.DoesNotExist):
11:24:04 web.1 | AttributeError: 'NoneType' object has no attribute 'model'
这是在以下情况中发生错误的视图:
class AddLocation(View):
template_name = "dash/Addlocation.html"
form = locationForm()
def get(self, request, *args, **kwargs):
user = User.objects.get(username=request.user.username)
self.form.fields['region_name'].queryset = Region.objects.filter(location__manager=user)
return render(request, self.template_name, {'form': self.form})
def post(self, request, *args, **kwargs):
form = locationForm(request.POST)
if form.is_valid():
region_obj, _created = Region.objects.get_or_create(name=form.cleaned_data['region_name'])
form.cleaned_data['region_name'] = region_obj
user = User.objects.get(username=request.user.username)
location = Location(
region_obj,
user,name = form.cleaned_data['name'],
street_address = form.cleaned_data['street_address'],
city = form.cleaned_data['city'],
zip_code = form.cleaned_data['zip_code']
)
location.save()
return redirect(reverse('location_manager'))
else:
form = locationForm(request.POST)
return render(request, self.template_name, {'form': form})
具体而言,似乎在尝试验证if form.is_valid():
如果需要,我可以发布更多信息,但我真的不知道这个错误是什么。有人可以对此有所了解吗?
编辑: 这是表单本身
from django import forms
class locationForm(forms.Form):
region_name = forms.ModelChoiceField(queryset=None, label="Region Name")
location_name = forms.CharField()
street_address = forms.CharField()
city = forms.CharField()
zip_code = forms.CharField()
答案 0 :(得分:6)
您已明确将region_name的查询集定义为None。这无效:您需要一个查询集。您可以在视图的get
方法中正确分配查询集,但不能在帖子中分配,因此当在帖子上验证表单时,它会失败。
在调用__init__
后,您应该覆盖表单的super
方法并在其中分配查询集,而不是在视图中执行此操作。