Django url path not routing properly
NoReverseMatch at /post/comment/1
Reverse for 'comment-reply' with arguments '('',)' not found. 1 pattern(s) tried: ['post/comment/reply/(?P<pk>[0-9]+)\\Z']
path('post/<slug:slug>/comment/create', CommentCreate.as_view(), name='post-comment'),
path('post/comment/reply/<int:pk>', ReplyCreate.as_view(), name='comment-reply'),
path('post/comment/<int:pk>', CommentDetailView.as_view(), name='comment-detail'),
class ReplyCreate(LoginRequiredMixin, CreateView):
"""
Form for adding a blog comment. Requires login.
"""
model = Reply
fields = ['description',]
def get_context_data(self, **kwargs):
"""
Add associated blog to form template so can display its title in HTML.
"""
# Call the base implementation first to get a context
context = super(ReplyCreate, self).get_context_data(**kwargs)
# Get the blog from id and add it to the context
context['comment'] = get_object_or_404(Comment2, pk = self.kwargs['pk'])
return context
def form_valid(self, form):
"""
Add author and associated blog to form data before setting it as valid (so it is saved to model)
"""
#Add logged-in user as author of comment
form.instance.author = self.request.user
#Associate comment with blog based on passed id
form.instance.comment=get_object_or_404(Comment2, pk = self.kwargs['pk'])
# Call super-class form validation behaviour
return super(ReplyCreate, self).form_valid(form)
def get_success_url(self):
"""
After posting comment return to associated blog.
"""
return reverse('detail2', kwargs={'slug': self.kwargs['slug'],})
class CommentDetailView(LoginRequiredMixin,generic.DetailView):
model = Comment2
comment2_detault.html:
{% extends 'blog/basedetail.html' %}
{% block content %}
<div class="card mb-3">
<div class="card-body">
<div class="card-title fw-bold">{{ comment.author }} | ({{ comment.post_date }})</div>
<p class="card-text">{{ comment.description }}</p>
</div>
</div>
<div class="d-flex justify-content-start align-items-center">
<h4 class="my-2 me-3"><i class="fa-solid fa-comments"></i> Reply</h4>
<a href="#replybox" class="text-success"><i class="fa-solid fa-pen-to-square fa-lg m-1"></i></a>
</div>
<ul class="list-group list-group-flush" style="height: 65vh; overflow-y: auto;">
{% for reply in comment.reply_set.all %}
<li class="list-group-item d-flex justify-content-between align-items-start shadow-sm m-2">
<div class="ms-2 me-auto">
<div class="d-flex justify-content-start m-1">
<div class="btn btn-secondary btn-sm mx-3"><i class="fas fa-user me-1"></i>{{ reply.author }}</div>
<div class="text-muted">({{ reply.post_date }})</div>
</div>
<p class="card-text mx-3 my-2">{{ reply.description }}</p>
<a href="{% url 'comment-reply' comment.id %}" class="btn btn-success btn-sm">Add a new Reply</a>
</div>
</li>
{% endfor %}
</ul>
<div class="d-flex justify-content-start align-items-center m-4" id="replybox">
{% if user.is_authenticated %}
<a href="{% url 'comment-reply' comment.id %}" class="btn btn-success btn-sm">Add a new Reply</a>
{% else %}
<a href="{% url 'login' %}?next={{ request.path }}" class="btn btn-success btn-sm">Login to add a new reply</a>
{% endif %}
<a href="{% url 'newblog9' %}" class="mx-3 btn btn-danger btn-sm"><i class="fa-solid fa-ban"></i></a>
</div>
{% endblock %}
Вернуться на верх