1. 程式人生 > >django搭建個人部落格10,文章詳細3-評論

django搭建個人部落格10,文章詳細3-評論

完善article_detail_base.html

增加評論展示以及上下篇導航欄

1.修改ArticleDetailView原生SQL執行語句,增加’上一篇’和’下一篇’導航項

def get_context_data(self,**kwargs):
        context=super(ArticleDetailView,self).get_context_data(**kwargs)
        artId=self.kwargs.get('article')
        artDate=tb_articles.objects.get(articleID=artId).created      context['comments'
]=tb_comments.objects.filter(articleID=artId).order_by(F('lefted').desc()) try: art=tb_articles.objects.raw(''' select articleID,title from www_tb_articles where created < %s order by created desc limit 1'''
,[artDate])[0] context['nextID']=art.articleID context['nextID']=''.join(str(context['nextID']).split('-')) context['nextTitle']=art.title except: context['nextID']="#" context['nextTitle']="沒有啦" try: art=tb_articles.objects.raw(''' select articleID,title from www_tb_articles where created >%s order by created asc limit 1'''
,[artDate])[0] context['prevID']=art.articleID context['prevID']=''.join(str(context['prevID']).split('-')) context['prevTitle']=art.title except: context['prevID']="#" context['prevTitle']="沒有啦" return context

2.修改article_detail_base.html,增加評論展示以及‘上一篇’,‘下一篇’導航標籤。

<div id="theComments">
{% for o in comments %}
<div class="aComment">
  <p>{{o.content}}</p>
  <p class="ip">{{o.IP}}</p>
  <p class="datetime">{{o.lefted}}</p>
</div>
{% endfor %}
</div>
<div class="sidebar">
      <h1>Useful Links</h1>
      <ul>
        {% if prevID != "#" %}
        <li><a href="{% url 'www:articleDetail' 0 prevID %}">上一篇: {{ prevTitle }}</a></li>
        {% else %}
        <li><a href="#">上一篇: {{ prevTitle }}</a></li>
        {% endif %}{% if nextID != "#" %}
        <li><a href="{% url 'www:articleDetail' 0 nextID %}">下一篇: {{ nextTitle }}</a></li>
        {% else %}
        <li><a href="#">下一篇: {{ nextTitle }}</a></li>
        {% endif %}
      </ul>
</div>

3.效果
導航欄