2017-09-15 73 views
1

我有一個系統設置,可以在帖子詳細信息頁面上顯示評論,但是不會顯示評論。我一直專注於模板標籤,因爲我在其他地方使用了這個視圖代碼,並且它已經工作,但是我可能是錯的。沒有錯誤返回,只是沒有在詳細視圖中顯示評論。Django評論沒有在模板中顯示

userpost_detail.html:

{% extends 'base.html' %} 

{% block content %} 

    <div class="main"> 
     <h1 class="posttitle">{{ userpost.title }}</h1> 

     <p class="postcontent">{{ userpost.post_body }}</p> 

     {% if request.user.is_authenticated and request.user == post.author %} 
      <a class="link" href="{% url 'feed:edit_post' post.id %}">Edit Post</a> 
     {% endif %} 


     <a href="{% url 'feed:add_comment' userpost.id %}">Add Comment</a> 

     {% for comment in userpost.usercomment.all %} 
      {% if user.is_authenticated %} 
       {{ comment.create_date }} 
       <!-- 
       <a class="btn btn-warning" href="{% url 'comment_remove' pk=comment.pk %}"> 
        <span class="glyphicon glyphicon-remove"></span> 
       </a> 
       --> 
       <p>{{ comment.comment_body }}</p> 
       <p>Posted By: {{ comment.author }}</p> 
      {% endif %} 
      {% empty %} 
      <p>No Comments</p> 
     {% endfor %} 
    </div> 

    {% include 'feed/sidebar.html' %} 

{% endblock %} 

應用PostDetailView:

class PostDetailView(DetailView): 
    model = UserPost 

應用add_comment_to_post視圖:

@login_required 
def add_comment_to_post(request,pk): 
    post = get_object_or_404(UserPost,pk=pk) 
    if request.method == 'POST': 
     form = CommentForm(request.POST) 
     if form.is_valid(): 
      comment = form.save(commit=False) 
      comment.post = post 
      comment.author = request.user 
      comment.save() 
      return redirect('feed:post_detail', pk=post.pk) 
    else: 
     form = CommentForm() 
    return render(request,'feed/comment_form.html',{'form':form}) 

應用網址:

from django.conf.urls import url 
from feed import views 

app_name = 'feed' 

urlpatterns = [ 
    url(r'^new/$',views.CreatePostView.as_view(),name='new_post'), 
    url(r'^post/(?P<pk>\d+)$',views.PostDetailView.as_view(),name='post_detail'), 
    url(r'^post/(?P<pk>\d+)/edit/$',views.UpdatePostView.as_view(),name='edit_post'), 
    url(r'^post/(?P<pk>\d+)/delete/$',views.DeletePostView.as_view(),name='delete_post'), 
    url(r'^post/(?P<pk>\d+)/comment/$',views.add_comment_to_post,name='add_comment'), 
] 

Models.py:

from django.db import models 
from django.core.urlresolvers import reverse 
from django.conf import settings 

from django.contrib.auth import get_user_model 
User = get_user_model() 

# Create your models here. 
class UserPost(models.Model): 
    author = models.ForeignKey(User,related_name='userpost',null=True) 
    post_date = models.DateTimeField(auto_now_add=True) 
    title = models.CharField(max_length=150,blank=False) 
    post_body = models.TextField(max_length=1000,blank=False) 

    def publish(self): 
     self.save() 

    def get_absolute_url(self): 
     return reverse('index') 

    def __str__(self): 
     return self.title 

class UserComment(models.Model): 
    post = models.ForeignKey('feed.UserPost',related_name='comments') 
    author = models.ForeignKey(User,related_name='usercomment') 
    comment_date = models.DateTimeField(auto_now_add=True) 
    comment_body = models.TextField(max_length=500) 

    def publish(self): 
     self.save() 

    def get_absolute_url(self): 
     return reverse("userpost_list") 

    def __str__(self): 
     return self.comment_body 
+0

我沒有看到在上下文中沒有'userpost'。 –

+0

上下文中是否是'user'? – schwobaseggl

+0

剛剛添加了'post_detail'網址。 – Garrett

回答

1

正如@ user6731765提到你需要comments堂妹的related_name

{% for comment in userpost.comments.all %} 

When you get comment_remove error

您需要定義一個URL爲comment_remove,並定義一個視圖。

urlpatterns = [ 
    . . . . . . 
    url(r'^comment/remove/(?P<pk>\d+)/$',views.DeleteCommentView.as_view(),name='comment_remove'), 
] 

然後在views.py

class DeleteCommentView(DeleteView): 
    model=UserComment 
0

由於related_name您在使用UserComment上崗,試着在你的模板使用

{% for comment in userpost.comments.all %} 

代替。

+0

給出了此錯誤:'NoReverseMatch at/feed/post/5 找不到'comment_remove'。 'comment_remove'不是有效的視圖函數或模式名稱 – Garrett

相關問題