After login not authenticated in Django

194 Views Asked by At

i wanted to create login functionality on my website. I did it with help of one tutorial (i use 'django.contrib.auth' app so i didn't create any own views yet), but when i log in, i get information that user is not logged (only authentication is not working because i see on admin panel i am logged succesfully). What should i change? My code:

login.html

<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Log In</button>
</form>

homepage.html

{% block content %}
{% if user.is_authenticated %}
  Hi {{ user.username }}!
{% else %}
  <p>You are not logged in</p>
  <a href="{% url 'login' %}">Log In</a>
{% endif %}
{% endblock %}

urls.py

from django.contrib import admin
from django.urls import include, path
from django.contrib.auth import views
from django.views.generic.base import TemplateView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('django.contrib.auth.urls')),
    path('', TemplateView.as_view(template_name='homepage.html'), name='homepage'),

settings.py

LOGIN_REDIRECT_URL = 'homepage'

Everything is working good, templates etc, but this authentication not, i get information "You are not logged in" which is displayed when user is not authenticated. Could someone help me? I do something wrong probably.

0

There are 0 best solutions below