轉到url時出現錯誤:/result_list.html。但views.py確實存在queryset,那麼可能還有其他什麼原因?提前致謝。該函數基於用戶提交的表單,然後過濾數據庫並在result_list.html中顯示錶單條目和數據庫結果。django 1.8:XView缺少QuerySet。定義XView.model,XView.queryset,
回溯
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\SCOR\result\views.py" in get_queryset
70. return super(ResultView,self).get_queryset()
文件 「C:\ Python27 \ LIB \站點包\ Django的1.8.3-py2.7.egg \ Django的\意見\通用\ list.py」 在get_queryset 44.'cls':自我。 class。 名稱
異常類型:錯誤地配置在/ result_list/ 異常值:ResultView缺少QuerySet。定義ResultView.model,ResultView.queryset或重寫ResultView.get_queryset()。
這裏下面是摘錄:
網址
from result.views import ResultView,InputFormView
from django.views.generic import TemplateView,FormView,ListView
urlpatterns = patterns('',
url(r'^result_list/$',ResultView.as_view(),name='result'),
url(r'^input/$',InputFormView.as_view(),name='input'),
)
views.py
from result.forms import InputForm
from result.models import Result,Input
from django.views.generic.list import ListView
from django.views.generic import FormView
....
@csrf_exempt
class InputFormView(FormView):
template_name = 'inputform.html'
form = InputForm
def get_success_url(self): /*redirect to result page with submitted form information*/
return ''.join(
[
reverse('dupont'),
'?company=',self.request.POST.get('company'),
'?region=',self.request.POST.get('region')
]
)
class ResultView(ListView):
context_object_name = 'result_list'
template_name = 'result_list.html'
def get_context_data(self, **kwargs):
context = super(ResultView, self).get_context_data(**kwargs)
return context
def get_queryset(self):
if self.request.method == 'POST':
form = InputForm(self.request.POST)
if form.is_valid():
company = form.cleaned_data['company']
region = form.cleaned_data['region']
/---Based on form entry, do the filter on the database-----/
queryset=Result.objects.filter(region=region,company=company)
sales=Result.objects.filter(queryset).aggregate(Sum('sales'))
employee=Result.objects.filter(queryset).aggregate(Sum('employee'))
departments=Result.objects.filter(queryset).aggregate(Sum('departments'))
form.save()
return render(request,'result_list.html',{'company':company},{'region':region},{'employee':employee},{'sales':sales},{'departments':departments})
else:
print form.errors
else:
form=InputForm()
return super(ResultView,self).get_queryset()
result_list.html
<div class="basicinfo"> <!--Entry Form information submitted by user-->
<table border="1" cellpadding="1">
<tr>
<td align="left">Company</td>
<td>{{company}}</td>
</tr>
<tr>
<td align="left">Region</td>
<td>{{region}}</td>
</tr>
</table>
<!--Showing the filtered result in database-->
<td><table border="0" cellspacing="10" cellpadding="10">
<tr><b>Sales</b></tr>
<td bgcolor="#F0F0F0"> {{sales}}</td>
</tr>
<tr><b>Employee</b></tr>
<tr>
<td bgcolor="#F0F0F0"> {{employee}}</td>
</tr>
<tr><b>Departments</b></tr>
<td bgcolor="#F0F0F0"> {{departments}}</td>
</td></table>
2.0版views.py按solarissmoke」和pythad的建議
class InputFormView(FormMixin,DetailView):
template_name = 'inputform.html'
form = InputForm
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form=self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
print form.errors
def form_valid(self, form):
company = form.cleaned_data['company']
region = form.cleaned_data['region']
return super(ResultView,self).form_valid(form) 《Q1:---If here to return to ResultView or InputFormView?--->
def get_success_url(self): #Redirect to result_list.html
return reverse('result', kwargs={'pk': self.object.pk})
class ResultView(MultipleObjectMixin,DetailView):
context_object_name = 'result_list'
template_name = 'result_list.html'
<---Q2: How can I get the form data entry from InputFormView?-->
def get(self, request, *args, **kwargs):
view = InputFormView.as_view()
self.object = self.get_object(queryset=Result.objects.filter(company=company,region=region))
return super(Result, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(ResultView, self).get_context_data(**kwargs)
context['InputFormView']= self.object
return context
def get_queryset(self):
<--Error!-- this sentence is where reports error as "global name company,region not defined"-->
queryset=Result.objects.filter(company=company,region=region)
sales=Result.objects.queryset.aggregate(Sum('sales'))
employee=Result.objects.queryset.aggregate(Sum('employee'))
departments=Result.objects.queryset.aggregate(Sum('departments'))
return self.object.all()
嗨@pythad,因爲有2個html和2個視圖,每個報告不同的錯誤,這就是爲什麼我在這裏發佈2個問題。非常感謝你的建議,你可以看看我升級的views.py嗎?它在更改後返回相同的錯誤。謝謝。 –