Reverse for 'edit_comment' with arguments '('', '')' not found. 1 pattern(s) tried: ['posts/(?P[^/]+)/edit_comment/(?P[^/]+)/\\Z']
Не открывается страница редактирования комментария. Выдает ошибку NoReverseMatch: Reverse for 'edit_comment' with arguments '('', '')' not found. 1 pattern(s) tried: ['posts/(?P<pk>[^/]+)/edit_comment/(?P<id>[^/]+)/\\Z']
Вот вью:
class AddCommentUpdateView(LoginRequiredMixin, UpdateView):
model = AddComment
form_class = AddCommentForm
pk_url_kwarg = 'id'
template_name = 'blog/comment.html'
post_date = None
def dispatch(self, request, *args, **kwargs):
instance = get_object_or_404(AddComment, pk=kwargs['id'])
if instance.author != request.user:
return redirect('blog:post_detail', pk=self.kwargs['pk'])
return super().dispatch(request, *args, **kwargs)
def get_success_url(self):
return reverse('blog:post_detail', kwargs={'pk': self.kwargs['pk']})
Модель
class AddComment(models.Model):
text = models.TextField('Текст комментария')
post = models.ForeignKey(
Post,
on_delete=models.CASCADE,
related_name='comment',
)
created_at = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
ordering = ('created_at',)
Урлы
path('posts/<pk>/edit_comment/<id>/',
views.AddCommentUpdateView.as_view(), name='edit_comment'),
Шаблон. Он без изменений. И его менять не нужно. Т.е. проблемы там по идее не должно быть
{% extends "base.html" %}
{% load django_bootstrap5 %}
{% block title %}
{% if '/edit_comment/' in request.path %}
Редактирование комментария
{% else %}
Удаление комментария
{% endif %}
{% endblock %}
{% block content %}
{% if user.is_authenticated %}
<div class="col d-flex justify-content-center">
<div class="card" style="width: 40rem;">
<div class="card-header">
{% if '/edit_comment/' in request.path %}
Редактирование комментария
{% else %}
Удаление комментария
{% endif %}
</div>
<div class="card-body">
<form method="post"
{% if '/edit_comment/' in request.path %}
action="{% url 'blog:edit_comment' comment.post_id comment.id %}"
{% endif %}>
{% csrf_token %}
{% if not '/delete_comment/' in request.path %}
{% bootstrap_form form %}
{% else %}
<p>{{ comment.text }}</p>
{% endif %}
{% bootstrap_button button_type="submit" content="Отправить" %}
</form>
</div>
</div>
</div>
{% endif %}
{% endblock %}