2016-09-18 135 views
0

我正在做一個項目在Django 1.9.9/python 3.5,由於練習的原因我有一個博客應用程序,一篇文章應用程序和一個評論應用程序。評論應用必須與博客和文章通用相關。我的問題是模板沒有顯示我的評論。評論正在創建並與他們的帖子/文章相關,因爲我可以在管理員中看到它,所以它不是評論創建問題。他們根本不在我的模板中顯示。評論沒有顯示在post_detail視圖

我的意見/ models.py:

from django.db import models 


class Comment(models.Model): 

    post = models.ForeignKey('blog.Entry',related_name='post_comments', blank=True, null=True) 
    article = models.ForeignKey('articles.Article', related_name='article_comments', blank=True, null=True) 
    body = models.TextField() 
    created_date = models.DateTimeField(auto_now_add=True) 

def __str__(self): 
    return self.body 

我commments/views.py:

from django.utils import timezone 
from django.shortcuts import render, get_object_or_404 
from django.shortcuts import redirect 

from .forms import CommentForm 
from .models import Comment 
from blog.models import Entry 
from articles.models import Article 
from blog.views import post_detail 
from articles.views import article_detail 



def article_new_comment(request, pk): 
    article = get_object_or_404(Article, pk=pk) 
    if request.method == "POST": 
     form = CommentForm(request.POST) 
     if form.is_valid(): 
      comment = form.save(commit=False) 
      comment.article = article 
      comment.created_date=timezone.now() 
      comment.save() 
      return redirect(article_detail, pk=article.pk) 
    else: 
     form=CommentForm() 
    return render(request, 'comments/add_new_comment.html', {'form': form}) 



def blog_new_comment(request, pk): 
    post = get_object_or_404(Entry, pk=pk) 
    if request.method == "POST": 
     form = CommentForm(request.POST) 
     if form.is_valid(): 
      comment = form.save(commit=False) 
      comment.post = post 
      comment.created_date = timezone.now() 
      comment.save() 
      return redirect(post_detail, pk=post.pk) 
    else: 
     form=CommentForm() 
    return render(request, 'comments/add_new_comment.html', {'form': form}) 

這裏是我的post_detail.html,其中的意見應該是。我不會發布article_detail.html因爲他們是完全一樣的:

{% extends 'blog/base.html' %} 
{% block content %} 
    <div class="post"> 
     {% if post.modified_date %} 
      <div class="date"> 
       {{ post.modified_date }} 
      </div> 
     {% else %} 
      <div class="date"> 
       {{ post.published_date }} 
      </div> 
     {% endif %} 
     <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"> Edit Post </span></a> 
     <h1>{{ post.title }}</h1> 
     <p>{{ post.text|linebreaksbr }}</p> 
     <hr> 
     <a class="btn btn-default" href="{% url 'new_blog_comment' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"> Add Comment </span></a> 
     {% for comment in post.comments.all %} 
      <div class="comment"> 
       <div class="date">{{ comment.created_date }}</div> 
       <p>{{ comment.body|linebreaksbr }}</p> 
      </div> 
     {% empty %} 
      <p>No comments here yet</p> 
     {% endfor %} 
    </div> 
{% endblock %} 

讓我知道如果任何其他文件將有助於你幫我,像博客/模型,視圖,雖然我不認爲這個問題在那兒。

回答

2

您已明確將帖子上的評論相關名稱設置爲post_comments。所以,你將不得不對其進行訪問,如:

{% for comment in post.post_comments.all %} 

這是在你的模板假設postEntry模型的實例。

+0

是的!非常感謝。我在Django中很新,所以有時候我找不到像這樣的明顯錯誤。 –

+0

沒問題。如果您在很多領域以類似的方式使用'Article'和'Entry'(例如,它們都附帶了Comment),請查看從基礎模型中繼承它們,然後將'Comment'外鍵基本模型,而不是每個都有一個字段。請參閱https://docs.djangoproject.com/en/1.10/topics/db/models/#multi-table-inheritance –