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 /」。我猜測上下文有個問題,但我不能把它放在手上。
任何幫助將不勝感激。
Typo? - 在視圖中的'notice_List',在模板中的'notice_list'。 – mariodev
ahh是的,我把它改成了另一個名字,看看它是否會在我發佈之前發揮作用,notice_List在模板中產生了相同的結果 –