I have a page which displays posts of all user and user can only delete his posts. Heres the code:
class PostDelete(generic.DeleteView):
model = Post
template_name = 'dashboard/post_delete.html'
success_url = reverse_lazy('dashboard:posts')
post_delete.html:
{% extends 'dashboard/sidebar.html' %}
{% block title %}Confirmation{% endblock %}
{% block mainpage %}
<div id="page-wrapper" align="center">
<div id="page-inner">
<h1>New post</h1>
<form method="post">
{% csrf_token %}
Are you sure you want to delete?
<br>
<button class="btn btn-danger">Yes</button>
<a href="{% url 'dashboard:posts' %}" class="btn btn-primary">No</a>
</form>
</div>
</div>
{% endblock %}
Urls.py:
path('delete/<int:pk>',views.PostDelete.as_view(),name='delete'),
How do I add user authentication code? If it were a function I would have used " if request.user.is_authenticated " But I don't know how to achieve this thing in a class. If you need an excerpt of another code then comment. Thanks!