Django模板在使用基于类的vi后没有显示大部分页面

2024-06-01 12:30:28 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是相关观点:

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>

当我导航到一个调用此视图的board url时,只显示左上角的第一个“/b/”。我猜上下文有点问题,但我无法确定

任何帮助都将不胜感激


Tags: nameselfboardurlgetobjectdefhtml
1条回答
网友
1楼 · 发布于 2024-06-01 12:30:28

实际上应该从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

相关问题 更多 >