Use Django's Authentication App part 1.
There
is a built-in app for auth in django that we can easy use to handle authentication.
We already have our Custom user model in our users app, so we will work inside that app to use the django default auth.
if you didn't read my post for creating custom user model
you can read it here
Create a new file called urls.py inside users app we will use the django auth views.
# urls.py
from django.contrib.auth import views as django_views
from django.urls import path
app_name = 'users'
urlpatterns = [
path('logout/', django_views.LogoutView.as_view(), name='logout'),
path('login/', django_views.LoginView.as_view(), name='login'),
]
Note: we imported views as django_views just to avoid any confusion when we create our custom views for profile home and profile edit pages.
And we will include this file into our main urls.
# django_tips/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("users.urls")),
]
Now we need to create our templates, by defult django auth login view will look to a template in the path registration/login.html so we will create that in the project root folder.
our folder structure will be something like this.
|__ Django_Tips_Project
|ــ django_tips
|ــ __init__.py
|ــ asgi.py
|ــ settings.py
|ــ urls.py
|ــ wsgi.py
|ــ templates
|ــ registration
|ــ login.html
|ــ base.html
|__ users
|__ migrations
|__ __init__.py
|__ admin.py
|__ apps.py
|__ forms.py
|__ models.py
|__ urls.py
|__ db.sqlite3
|__ manage.py
|__ requirements.txt
We will need to edit settings.py to add our templates path.
# settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Now let's edit our templates, we created a base.html file to use in all our future templates.
# base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta charset="utf-8">
<title>Django Tips</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
And then our login.html file like this.
# login.html
{% extends 'base.html' %}
{% block title %} | Login {% endblock %}
{% block content %}
<div>
{% if user.is_authenticated %}
<div>
<p>You are logged in</p>
<a href="/">Home Page</a>
</div>
{% else %}
<div>
<h2>Login</h2>
<div>
<form method="post" action="{% url 'users:login' %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">login</button>
</form>
</div>
</div>
{% endif %}
</div>
{% endblock %}
we checked first if the user is already logged in because we don't need the logged in user to see the login form.
last thing we need to do is to edit the redirect urls in settings.py file where we added the AUTH_USER_MODEL we will add LOGIN_REDIRECT_URL and LOGOUT_REDIRECT_URL there.
# settings.py
# Custom User settings
AUTH_USER_MODEL = 'users.CustomUser'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = 'users:login'
And all is set! you can login at http://127.0.0.1:8000/login/ and log out at http://127.0.0.1:8000/logout/
Next post I will show you a small bug in the django login view and a way to solve it :).
I hope the tutorial was simple.
Feel free to reach out if you have any questions.
Join to discord Server Here - Django Learn Together.
Views: 454