Protecting View
We have to start protecting our views against non-authorized users. So far we have the following view to start new posts:
Views.py
from django.contrib.auth.decorators import login_required
@login_required
def new_topic(request, pk):
passIf User Logged In
Views.py
from django.contrib.auth import login, authenticate, logout
if request.method == 'GET':
if request.user.is_authenticated:
return redirect('posts')Html Template
Example 1 : Show Login/Logout Button Based On User Loggin/logout
{%if request.user.is_authenticated %}
<span>Hi {{ request.user.username | title }}</span>
<a href="{% url 'logout' %}">Logout</a>
{%else%}
<a href="{% url 'login' %}">Login</a>
{%endif%}
Example 2 : Show post Delete / Edit Option Based On User Loggin/Logout
Only Owner Can Delete/Edit Post
Last updated