2015-10-14 16 views
0

我想展示2款(輸入;結果)在一個HTML模板(結果):Django的錯誤:顯示一個HTML兩種模式時

的錯誤是「ResultView」沒有定義。如果下面有任何錯誤,請糾正我。預先感謝您的建議。

結果 - Views.py

from result.models import Result 
from inputform.models import Input 

class ResultView(ListView): 
    context_object_name = 'result_list' 
    template_name = 'result_list.html' 
    queryset = Result.objects.all() 

    def get_context_data(self, **kwargs): 
     context = super(ResultView, self).get_context_data(**kwargs) 
     context['input'] = Input.objects.all() 
     return context 

網址

from django.views.generic.list import ListView 
from result.views import ResultView 

    urlpatterns = patterns('', 

    url(r'^result_list/$',ResultView.as_view(),name='dupont'), 
    url(r'^input', 'inputform.views.input',name='input'), 
) 

result_list --- HTML

<div class="basicinfo">   <!--Input information--> 
    {% for input in input_list %} ------If here correct? 
     <table border="1" cellpadding="1"> 
     <tr> 
      <td align="left">Company</td> 
      <td>{{input.company}}</td> 
     </tr> 
     <tr> 
     </table> 
    {% endfor %} 
</div> 

<div class="result">   <!--Result information--> 
    {% for result in Result_list %}    ----If here correct? 
     <table border="1" cellpadding="1"> 
     <tr> 
      <td align="left">Totao</td> 
      <td>{{result.Total}}</td> 
     </tr> 
     <tr> 
     </table> 
    {% endfor %} 
+0

發現在'網址拼寫錯誤(R'^ result_list/$',ReultView.as_view(),name ='dupont')'這是'結果'而不是'Reult'。另外,你是否在你的urls.py中導入了ResultView? – Cheng

+0

您是否將ListView導入到您的views.py中? – pythad

+0

是的,我已經導入了ListView。 –

回答

1

你有一個拼寫錯誤你的urls.py。您嘗試使用ReultView而不是ResultView。 要訪問您的模板中的數據,您必須使用您的上下文名稱。如果添加Input對象爲context['input'] = Input.objects.all(),你需要循環它以這樣一種方式:

<div class="basicinfo"> 
    {% for input_object in input %} 
     <table border="1" cellpadding="1"> 
      <tr> 
       <td align="left">Company</td> 
       <td>{{input_object.company}}</td> 
      </tr> 
     </table> 
    {% endfor %} 
</div> 

你result_list的名字是result_list不Result_list:

<div class="result"> 
    {% for result in result_list %} 
     <table border="1" cellpadding="1"> 
      <tr> 
       <td align="left">Totao</td> 
       <td>{{result.Total}}</td> 
      </tr> 
     </table> 
    {% endfor %} 
</div> 
+0

謝謝你,這正是我所困惑的。 –

1

您已經導入ListVie而不是ResultView到你的urls.py中。

1
  1. 您需要導入ResultViewurls.py
  2. 您評爲context_object_nameresult_list,當你在result_list.html引用它,你應該堅持result_list而不是使用Result_list
  3. beca使用你寫context[input]=...,輸入列表的名稱是輸入,而不是input_list所以這意味着當你在模板中訪問它,你應該使用:

    {% for item in input %} 
        ...