Не работает определение нужного юзера для редактирования комментария
решил значит я добавить функционал редактирования и удаления комментариев на своем сайте. Проблема в том что при проверке правильного юзера который создавал пост, исчезают сами кнопки, вне зависимости от автора поста, то-есть даже у самого автора не выводятся кнопки полностью испаряются.
views.py:
class AddComment(CreateView):
model = Comment
form_class = CommentForm
template_name = 'WebPortfolioApp/add_comment.html'
def form_valid(self, form):
form.instance.project = PortfolioStructure.objects.get(slug=self.kwargs['proj_slug'])
return super().form_valid(form)
def get_success_url(self):
return reverse('project', kwargs={'proj_slug': self.object.project.slug})
class EditComment(UpdateView):
model = Comment
form_class = CommentForm
template_name = 'WebPortfolioApp/edit_comment.html'
def get_success_url(self):
return reverse('project', kwargs={'proj_slug': self.object.project.slug})
class DeleteComment(DeleteView):
model = Comment
template_name = 'WebPortfolioApp/delete_comment.html'
def get_success_url(self):
return reverse('project', kwargs={'proj_slug': self.object.project.slug})
urls.py:
urlpatterns = [
path('', ListProjects.as_view(), name='home'),
path('project/<slug:proj_slug>/', ProjectDetail.as_view(), name='project'),
path('login/', LoginUser.as_view(), name='login'),
path('logout/', logout_user, name='logout'),
path('register/', RegisterUser.as_view(), name='register'),
path('project/<slug:proj_slug>/like', like_view, name='like_project'),
path('project/<slug:proj_slug>/add_comment/', AddComment.as_view(), name='add_comment'),
path('project/comment/<int:pk>/delete', DeleteComment.as_view(), name='delete_comment'),
path('project/comment/<int:pk>/edit', EditComment.as_view(), name='edit_comment'),
]
models.py:
class Comment(models.Model):
project = models.ForeignKey(PortfolioStructure, related_name='comments', on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = models.TextField()
commentator = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.project.title, self.name)
html:
<h2 class="mb-4">Comments...</h2>
{% if not project.comments.all %}
No Comments Yet...<div class="mb-4"><a class="link-primary" href="{% url 'add_comment' project.slug %}">Add comment</a></div>
{% else %}
{% if user.is_authenticated %}
<div class="mb-3"><a class="link-primary" href="{% url 'add_comment' project.slug %}">Add comment</a></div>
{% else %}
<div class="mb-3"><a class="link-primary" href="{% url 'login' %}">Login to add comment</a></div>
{% endif %}
{% for comment in project.comments.all %}
<strong>
{{ comment.name }} -
{{ comment.date_added }}
{% if user == comment.commentator %}
<a class="fw-normal link-primary" href="{% url 'edit_comment' comment.pk %}">[edit]</a>
<a class="fw-normal link-primary" href="{% url 'delete_comment' comment.pk %}">[delete]</a>
{% endif %}
</strong>
<br>
<p>{{ comment.body }}</p>
<br>
<br>
{% endfor %}
{% endif %}
Источник: Stack Overflow на русском