我收到错误#34;未定义全局名称区域"在" queryset = Result.objects.filter(region = region)"的行中。
错误可能是因为我不应该验证查询集结果是否在数据库中有相应的记录" def clean"。
所以我试图删除" def clean"并将此验证放在def get_queryset(第2个views.py)中。然后,如果我在dababase中输入没有查询集结果的内容,它就不会在表单页面上报告错误。
有什么帮助可以找到我如何更改代码?提前谢谢。
views.py
class ResultView(ListView):
context_object_name = 'result_list'
template_name = 'result_list.html'
model = Result
def clean(self):
cleaned_data = super(MyForm, self).clean()
region = form.cleaned_data['region']
country= form.cleaned_data['country']
if not Result.objects.filter(region=region,country=country).exists():
self.add_error(ValidationError('No corresponding data exists'))
return cleaned_data
def get_queryset(self):
form = InputForm(self.request.GET)
queryset=Result.objects.filter(region=region) -//here wrong
return queryset
return Result.objects.all()
def get_context_data(self,**kwargs):
context["sales"] = self.get_queryset().aggregate(Sum('sales'))
views.py - 如果删除def clean - 错误功能不起作用
class ResultView(ListView):
context_object_name = 'result_list'
template_name = 'result_list.html'
model = Result
def get_queryset(self):
form = InputForm(self.request.GET)
if form.is_valid():
country = form.cleaned_data['country']
region = form.cleaned_data['region']
if country !="" and region !="":
if Result.objects.filter(region=region,country=country).exists():
try:
queryset=Result.objects.filter(region=region,country=country)
except:
self.add_error(ValidationError('No corresponding data exists'))
return queryset
return Result.objects.all()
def get_context_data(self,**kwargs):
context["sales"] = self.get_queryset().aggregate(Sum('sales'))
HTML代码段
<form method="post">{% csrf_token %}
{% csrf_token %}
{{ formset.management_form }}
{{ formset.errors }}
{{ formset.non_field_errors }}
{{ formset.non_form_errors }}
{{ form.non_field_errors }}
......
<!--region--> --it allows to select none
<div class="field {% if field.errors %} field_error{% endif %}" >
<label> Select Region:
{{ form.region }}
{% for region in form.region.choices %}
<option value="region" name= "region" id="id_region">{{region}} </option>
{% endfor %}
</label>
</div>
我没有添加&#34; {{form.non_field_errors}}&#34;因为它允许空白的某些字段
回溯
Traceback:
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\generic\list.py" in get
159. self.object_list = self.get_queryset()
File "C:\Users\user\Desktop\XXX\result\views.py" in get_queryset
61. Result.objects.filter(region=region,country=country).exists()
答案 0 :(得分:3)
这很明显:
form = InputForm(self.request.GET)
queryset=Result.objects.filter(region=region)
在此范围内,当然没有定义region
。
您需要从表单中检索区域,对其进行清理,然后使用它进行查询
form = InputForm(self.request.GET)
if form.is_valid():
queryset=Result.objects.filter(region=form.cleaned_data['region'])
进一步说,应该是:
def get_queryset(self):
form = InputForm(self.request.GET)
if form.is_valid():
country = form.cleaned_data['country']
region = form.cleaned_data['region']
if country !="" and region !="": # <--- moved one intend to right
if Result.objects.filter(region=region,country=country).exists():
try:
queryset=Result.objects.filter(region=region,country=country)
except:
self.add_error(ValidationError('No corresponding data exists'))
return queryset
return Result.objects.all()