2013-08-24 119 views
1

這裏的相關視圖後頁面的大部分:Django的模板沒有顯示使用基於類的視圖

class board_lv(generic.ListView): 
    template_name = 'boardList.html'  
    context_object_name = 'notice_List' 
    def get_queryset(self): 
     self.Board = get_object_or_404(Board, name=self.args[0]) 
     if self.args[0] == 'all':   
      return Notice.objects.order_by('-posted_on') 
     else: 
      return Notice.objects.filter(board=self.Board) 

    def get_context_data(self, **kwargs): 
     context = super(board_lv, self).get_context_data(**kwargs) 
     context['c_board'] = self.Board; 

這裏是HTML模板:

<h1>/b/ {{c_board}}</h1> 
<ul> 
{% for n in notice_list %} 
    <li> 
    {% if not n.isText %} 
     <h2><a href="{{ n.content }}">{{ n.title }}</a></h2>(/b/{{n.board}})<br>  
    {% else %} 
     <h2><a href= "{% url 'detail' n.id %}">{{ n.title }}</a></h2>(/b/{{n.board}}) 
     <p>{{ n.content|slice:":100" }}</p>  
    {% endif %} 
     <a href="{% url 'detail' n.id %}">Comments</a>{{n.thumbs_up}} 
    </li> 
{% endfor %} 
</ul> 

當我瀏覽到一個板網址它調用這個視圖,所有顯示的是左上角的第一個「/ b /」。我猜測上下文有個問題,但我不能把它放在手上。

任何幫助將不勝感激。

+0

Typo? - 在視圖中的'notice_List',在模板中的'notice_list'。 – mariodev

+0

ahh是的,我把它改成了另一個名字,看看它是否會在我發佈之前發揮作用,notice_List在模板中產生了相同的結果 –

回答

0

實際上您應該從get_context_data返回上下文!所以改變它是這樣的

 

def get_context_data(self, **kwargs): 
     context = super(board_lv, self).get_context_data(**kwargs) 
     context['c_board'] = self.Board; 
     return context 

+0

它的工作!謝謝! –

相關問題