I am developing an app in Django.
I am developing users authentication.
I have a registration.html and a login.html templates inside path: templates > authentication
Everything, including the registering function, works fine, but as I try to access to login template, the browser returns:
NoReverseMatch at /login
'app' is not a registered namespace
I bet the problem lies in the LOGIN_URL
that I added in settings.py to enable authentication system (I am following a tutorial).
In fact, all the others view work fine, just the one pointing to login.html is not.
Here below are all my lines relating to the authentication system:
In my settings.py:
LOGIN_URL = '/login'
In my base.html:
{% if user.is_authenticated %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">{{ user_form.username }}</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="">profilo</a>
<a class="dropdown-item" href="{% url 'logout' %}">Log out</a>
</div>
</li>
{% elif not user.is_authenticated %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Login</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="{% url 'registration' %}">Registrati</a>
<a class="dropdown-item" href="{% url 'login' %}">Accedi</a>
</div>
</li>
{% endif %}
In my authentication > login.html:
{% extends 'base.html'%} <!-- vuol dire inserisci qui la navigation toolbar contenuta in base -->
{% block content %}
<h1>Login</h1>
<br>
<div class="jumbotron">
<form action="{% url 'app:login' %}" method="post">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" name="username" value="" placeholder="nome utente">
<label for="password">Password:</label>
<input type="password" name="password" value="" placeholder="password">
<input type="submit" name="" value="Login">
</form>
</div>
{% load static %} <!-- Qui il tag è obbligatorio nonostante sia stato inserito dentro base.html -->
<!-- CSS -->
{% comment %} <link rel="stylesheet" type="text/css" href={% static "css/file.css" %}> {% endcomment %}
<!-- Javascript -->
{% comment %} <script type="text/javascript" src={% static "js/file.js" %}></script> {% endcomment %}
{% endblock %}
In my app > urls.py, Inside urlpatterns
list:
path('authentication/registration', views_users_authentication.registration, name="registration"),
path('login', views_users_authentication.user_login, name="login"),
in my project > urls.py, Inside urlpatterns
list:
path('admin/', admin.site.urls),
path('', include('app.urls')),
Then I have a separate sheet to contain views functions related to the authentication system, that is views_users_authentication.py , which contains:
def registration(request):
registered = False
# se l'utente ha lanciato il post
if request.method=="POST":
print("post eseguito!")
user_form = UserForm(data=request.POST)
profile_form = UserProfileInfoForm(data=request.POST)
# condizione di validità del form
if user_form.is_valid() and profile_form.is_valid():
print("form validi!")
user = user_form.save()
user.set_password(user.password) # questa linea hasha la pasword
user.save()
# registra l'utente
profile = profile_form.save(commit=False)
profile.user = user
registered=True
print("Utente registrato con successo!")
# condizione per registrare l'utente
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']
print("Acquisita la fotografia dell'utente!")
profile.save()
# attenzione al salvataggio dei form e dei modelli che sono due cose diverse
# registra le info aggiuntive
else:
print("Registrazione fallita:")
print(user_form.errors, profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileInfoForm()
context_dict = {'user_form':user_form, 'profile_form':profile_form, 'registered':registered}
return render(request, 'authentication/registration.html', context_dict)
def user_login(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('home'))
else:
HttpResponse("Account non attivo")
else:
print("qualcuno ha cercato di loggarsi e ha fallito")
print("Username: {} and password {}".format(username,password))
return HttpResponse("Inseriti parametri non validi per il login!")
else:
return render(request, "authentication/login.html", {})
In your login.html
you should use just login
as url name instead of app:login
:
<form action="{% url 'login' %}" method="post">
Since you didn't specify namespace in urlpatterns.py file. If you want to use app
namespace you can change urlpattern like this:
path('', include('app.urls', namespace='app')),