表格未显示。我应该如何在一个页面中显示多个表单?我想写评论&回复系统。
这是 comment.html
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>COMMENT</title>
</head>
<body>
<div>
<h2>{{ object.text }}</h2>
</div>
<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 %}"Reply</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 %}
</div>
</div>
{% endfor %}
</div>
<form action="" method="POST" enctype='multipart/form-data'>
{% for field in form %}
<div class="form-group">
{{ field.errors }}
<label for="{{ field.id_for_label }}">
{{ field.label }}
</label>
{{ field }}
</div>
{% endfor %}
{% csrf_token %}
<input class="btn btn-primary" type="submit" value="comment">
</form>
</body>
</html>
在 views.py 中,我有三个观点
class DetailView(generic.DetailView):
model = Post
template_name = 'comment.html'
class CommentCreateView(generic.CreateView):
model = Comment
form_class = CommentCreateForm
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['post_pk'] = self.kwargs['pk']
return context
def form_valid(self, form):
post_pk = self.kwargs['pk']
self.object = form.save(commit=False)
self.object.target = Post.objects.get(pk=post_pk)
self.object.save()
return redirect('detail', pk=post_pk)
class ReCommentCreateView(generic.CreateView):
model = ReComment
form_class = ReCommentCreateForm
template_name = 'comment.html'
def get_context_data(self, *args, **kwargs):
comment_pk = self.kwargs['pk']
comment = Comment.objects.get(pk=comment_pk)
context = super().get_context_data(*args, **kwargs)
context['post_pk'] = comment.target.pk
return context
def form_valid(self, form):
comment_pk = self.kwargs['pk']
comment = Comment.objects.get(pk=comment_pk)
self.object = form.save(commit=False)
self.object.target = comment
self.object.save()
return redirect('detail', pk=comment.target.pk)
urls.py 中的
urlpatterns = [
path('comment/<int:pk>/', views.DetailView.as_view(template_name='comment.html'), name='comment'),
]
当我访问http://localhost:8000/polls/comment/1/
时,不会显示任何表单。我认为CommentCreateView
&amp; views.py中的ReCommentCreateView
类与任何网址都没有关联,但我希望这些表单显示在comment.html中。我该怎么做?
答案 0 :(得分:0)
我假设您要对
Post
模型的详细信息页面上的特定评论创建评论并回复(重新评论)。
尽管为 CommentCreateView 和 ReCommentCreateView 类创建View
并覆盖CreateView
方法,您仍可以创建post
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)
现在在 DetailView 类的上下文数据中发送ReCommentCreateForm
和CommentCreateForm
。
class DetailView(generic.DetailView):
model = Post
template_name = 'comment.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['comment_form'] = CommentCreateForm()
context['recomment_form'] = ReCommentCreateForm()
return context
在您的comment.html中,您可以使用这两个变量来呈现表单。
# comment form action url should have id of post.
<form action="{% url 'comment' post_pk=post.id %}" method="post">
{{comment_form}}
{% csrf_token %}
<input class="btn btn-primary" type="submit" value="comment">
</form>
# re-comment form action url should have id of comment on which user is replying.
<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>
更新以下所有观看的网址。
urlpatterns = [
# Detail page of post url
path('post/<int:pk>/', views.DetailView.as_view(), name='post'),
# CommentCreateView page url to create comment on post
path('comment/<int:post_pk>/', views.CommentCreateView.as_view(), name='comment'),
# ReCommentCreateView page url to create re-comment on comment
path('recomment/<int:comment_pk>/', views.ReCommentCreateView.as_view(), name='recomment'),
]
使用以下方式访问您的帖子详情页面:
http://localhost:8000/polls/post/1/