当我放置评论按钮时没有发生任何事情。我想制作一个显示评论和推荐的页面。我在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'),
]
in detail.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)
,正确的值显示在终端中。我的代码出了什么问题?我应该如何解决这个问题?
答案 0 :(得分:0)
您是否拥有在DB中实际创建的对象?无论如何,帖子中缺少一些信息(如表格本身等):
如果您未在外键上设置related_name
参数,则可以通过post.comment_set.all()
正确访问帖子的评论,请参阅:
https://docs.djangoproject.com/en/2.0/topics/db/queries/#following-relationships-backward
我可以指出你正在做很多工作,Django的通用视图可以为你做(如果你正在学习,你可以猜测它是没关系的,否则,只需要维护更多的代码)。
来自CreateView
的{{1}},UpdateView
和DeleteView
等内容。有关详细信息,请参阅:
https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-editing/#model-forms