2015-10-21 69 views
0

轉到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() 
+0

嗨@pythad,因爲有2個html和2個視圖,每個報告不同的錯誤,這就是爲什麼我在這裏發佈2個問題。非常感謝你的建議,你可以看看我升級的views.py嗎?它在更改後返回相同的錯誤。謝謝。 –

回答

3

有多個問題與您的代碼:

  1. 您的get_queryset方法的if self.request.method == 'POST':塊。如果計算結果爲False,那麼您將回到父方法get_queryset,這是觸發錯誤的原因(因爲父錯誤方法需要提供模型或查詢集,如錯誤中所示)。所以如果你嘗試一個簡單的GET請求到result_list/它會失敗,因爲ListView沒有查詢集。

  2. 您不應該從get_queryset方法中渲染模板。該方法應該返回一個查詢集,而不是別的。這將失敗。

  3. 您的ResultView確實不應該首先處理POST的表單數據。您應該在FormView中處理該問題,然後使用documentation on form processing中解釋的參數FormView的參數將用戶重定向到適當的結果視圖。

+0

嗨@solarissmoke,非常感謝您的建議。我改變了看法。py根據你的建議,但仍然有1個問題我不明白。你可以看看我的「根據你的建議更新views.py」嗎?提前致謝。 –

+0

您需要將相關參數傳遞給您的結果視圖,然後在該視圖中執行查詢集邏輯(而不是在窗體視圖中)。 – solarissmoke

+0

嗨@ solarissmoke,謝謝。我上傳了「Verson 2.0 views.py」,但是我得到了「全局名稱」公司「未定義」的錯誤,所以我懷疑來自InputFormView的表單入口數據沒有成功地放到ResultView上。我在片段中做了筆記,你可以看看嗎?非常感謝。 –