当我放置评论按钮时,什么也没发生

  • 本文关键字:评论 按钮 python django
  • 更新时间 :
  • 英文 :


当我放置评论按钮时没有任何反应。我想制作一个显示评论和重新评论的页面。我用 views.py 编写代码

class DetailView(generic.DetailView):
   model = POST
   template_name = 'detail.html'
   def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['comment_form'] = CommentCreateForm()
    context['recomment_form'] = ReCommentCreateForm()
    return context 
class CommentCreateView(generic.View):
    def post(self, request, *args, **kwargs):
        form = CommentCreateForm(request.POST)
        post = POST.objects.get(pk=kwargs['post_pk'])
        if form.is_valid():
            obj = form.save(commit=False)
            obj.target = post
            obj.save()
        return redirect('detail', pk=post.pk)

class ReCommentCreateView(generic.View):
    def post(self, request, *args, **kwargs):
        form = ReCommentCreateForm(request.POST)
        comment = Comment.objects.get(pk=kwargs['comment_pk'])
        if form.is_valid():
            obj = form.save(commit=False)
            obj.target = comment
            obj.save()
        return redirect('detail', pk=comment.target.pk)

在 urls.py

from django.urls import path
from django.conf import settings
from . import views
urlpatterns = [
    path('detail/<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('comment/<int:post_pk>/',views.CommentCreateView.as_view(), name='comment'),
    path('recomment/<int:comment_pk>/', views.ReCommentCreateView.as_view(), name='recomment'),
]

详细内容.html

{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DETAIL</title>
</head>
<body>
<div id="comment-area">
    {% for comment in post.comment.all %}
    <div class="media m-3">
        <div class="media-body">
            <h5 class="mt-0">
                <span class="badge badge-primary badge-pill">{% by_the_time comment.created_at %}</span>
                {{ comment.name }}
                <span class="lead text-muted">{{ comment.created_at }}</span>
                <a href="{% url 'recomment' comment.pk %}">Recomment</a>
            </h5>
            {{ comment.text | linebreaksbr }}
            {% for recomment in comment.recomment.all %}
            <div class="media m-3">
                <div class="media-body">
                    <h5 class="mt-0">
                        {{ recomment.name }}
                        <span class="lead text-muted">{{ recomment.created_at }}</span>
                    </h5>
                    {{ recomment.text | linebreaksbr }}
                </div>
            </div>
            {% endfor %}
            <form action="{% url 'recomment' comment_pk=comment.id %}" method="post">
                {{recomment_form}}
                {% csrf_token %}
                <input class="btn btn-primary" type="submit" value="re-comment">
            </form>
        </div>
    </div>
    {% endfor %}
    <form action="{% url 'comment' post_pk=post.id %}" method="post">
        {{comment_form}}
        {% csrf_token %}
        <input class="btn btn-primary" type="submit" value="comment">
    </form>
</div>
</body>
</html>

在 models.py

class Comment(models.Model):
    name = models.CharField(max_length=100, blank=True)
    text = models.TextField()
    target = models.ForeignKey(POST, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.name

当我放置评论按钮时,没有发生错误,但显示表单内容的同一页面是空白的。我想展示{% for comment in post.comment.all %}~{% endfor %},所以我真的不明白为什么我不能这样做。我认为值不在模型中,所以我在 CommentCreateView 的类中打印出print(obj),并且正确的值显示在终端中。我的代码出了什么问题?我应该如何解决这个问题?

您是否在数据库中实际创建了对象?无论如何,帖子中缺少一些信息(例如表单本身等(:

如果您没有在外键上设置related_name参数,则正确访问 post 的评论是通过 post.comment_set.all() ,请参阅:

https://docs.djangoproject.com/en/2.0/topics/db/queries/#following-relationships-backward

我可以指出,你正在做很多工作,Django 的通用视图可以为你做(如果你正在学习,猜猜没关系,否则,它只是需要更多的代码来维护(。

诸如CreateViewUpdateViewDeleteView之类的东西来自django.views.generic.edit。有关详细信息,请参阅:

https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-editing/#model-forms

最新更新