……………..urls.py……….
from django.contrib import admin
from django.urls import path, include
from user import views as user_view
from django.contrib.auth import views as auth_views
from django.views.generic import TemplateView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dashboard.urls')),
path('register/', user_view.register, name = 'user-register'),
path('profile/', user_view.profile, name = 'user-profile'),
path('', auth_views.LoginView.as_view(template_name = 'user/login.html'),name='user-login'),
path('logout/', TemplateView.as_view(template_name = 'user/logout.html'),name='user-logout' ),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
………..views.py……..
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from .forms import CreateUserForm
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
def register(request):
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
return redirect('user-login')
else:
form = CreateUserForm()
context = {
'form': form,
}
return render(request, 'user/register.html',context)
def profile(request):
return render(request, 'user/profile.html')
………nav.html……..
<nav class="navbar navbar-expand-lg navbar-info bg-info">
{% if user.is_authenticated %}
<div class="container">
<a class="navbar-brand text-white" href="{% url 'dashboard-index' %}">NTPC Inventory</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link text-white" href="{% url 'dashboard-index' %}">Dashboard <span
class="sr-only">(current)</span></a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link text-white" href="profile.html">Admin Profile <span
class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link text-white" href="{% url 'user-logout' %}">Logout</a>
</li>
</ul>
<!-- problem is not solved as is.authenticated is not working as it show logout and admin profile for the same still finding out the solution -->
{% else %}
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link text-white" href="{% url 'user-register' %}">Register <span
class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link text-white" href="{% url 'user-login' %}">Login</a>
</li>
</ul>
{% endif %}
</div>
</div>
</nav>
I have tried importing different libraries but still unable to find the solution, whenever I clear the cookies of chrome it works for that single load of page only once i reload, it again comes to the same .
It shows the authenticated block at the nav as
Admin profile logout
but doesn’t show register login option.
New contributor
Sushil Sonawane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.