A "My profile" page
Make a protected page /nalozi/profil/ that shows the username, the registration date, the number of saved logos and a link for changing the password.
We do not write a view for signing in and out – Django has them ready (LoginView, LogoutView). It is enough to include their addresses and make a template. Then we lock the pages that only signed-in users may see.
The line path("nalozi/", include("django.contrib.auth.urls")) in the main urls.py includes:
| Address | Route name | What it does |
|---|---|---|
/nalozi/login/ | login | the sign-in form |
/nalozi/logout/ | logout | signing out (POST only) |
/nalozi/password_change/ | password_change | changing the password |
/nalozi/password_reset/ | password_reset | a forgotten password (requires sending email) |
Where you go after signing in and out is set by LOGIN_REDIRECT_URL and LOGOUT_REDIRECT_URL from lesson 3; LOGIN_URL = "login" says where a user who is not signed in is sent. (nalozi = accounts – the name of the app in this project.)
{# templates/registration/login.html – Django's LoginView looks for this name and place #}
{% extends "base.html" %}
{% block naslov %}Sign in – Logo Creator{% endblock %}
{% block sadrzaj %}
<div class="container py-4" style="max-width: 420px">
<h1 class="h3 mb-3">Sign in</h1>
{% if form.non_field_errors %}
<div class="alert alert-danger">Wrong username or password.</div>
{% endif %}
<form method="post">
{% csrf_token %}
<div class="mb-3">
<label for="id_username" class="form-label">Username</label>
<input type="text" name="username" id="id_username" class="form-control" required autofocus>
</div>
<div class="mb-3">
<label for="id_password" class="form-label">Password</label>
<input type="password" name="password" id="id_password" class="form-control" required>
</div>
<input type="hidden" name="next" value="{{ next }}">
<button class="btn btn-danger w-100">Sign in</button>
</form>
<p class="mt-3 small">No account? <a href="{% url 'registracija' %}">Register</a>.</p>
</div>
{% endblock %}
LoginView looks for the template registration/login.html and gives it the form under the name form and the return address next. If the data is wrong, form.non_field_errors is not empty. The message deliberately does not say whether the name or the password is wrong – that way an attacker cannot find out which usernames exist.
Since Django 5.0, signing out works only with a POST request. If a link (GET) were enough, someone else's page could sign you out with a hidden image pointing to that address (CSRF). That is why signing out in base.html is a small form with a button:
<form method="post" action="{% url 'logout' %}" class="d-inline">
{% csrf_token %}
<button class="btn btn-link nav-link">Sign out ({{ user.username }})</button>
</form>
Old examples with <a href="/logout/"> return the error 405 (Method Not Allowed) in new versions.
from django.contrib.auth.decorators import login_required
@login_required # a user who is not signed in goes to /nalozi/login/?next=/moji-logoi/
def moji_logoi(request):
logos = request.user.logoi.all()
return render(request, "logoi/moji_logoi.html", {"logos": logos})
| Check | Where | Example |
|---|---|---|
@login_required | view | the whole page only for signed-in users |
request.user.is_authenticated | view | the same view works differently (generator: saving only for signed-in users) |
{% if user.is_authenticated %} | template | what is shown in the menu |
get_object_or_404(..., vlasnik=request.user) | view | a user gets only their own data (lesson 8) |
In the video the sign-in view is written by hand (authenticate + login). That is good for understanding, but the ready-made LoginView is shorter and safer.
Each task has an example, a hint and a solution.
Make a protected page /nalozi/profil/ that shows the username, the registration date, the number of saved logos and a link for changing the password.
templates/registration/login.html.next on to other people's sites – Django's LoginView checks this itself, a view written by hand must check it too.1. Why does signing out in Django 5 work only with a POST request?
2. What does @login_required do?
3. Is it enough to hide the link in the menu for a page to be protected?