top of page

Create a login/logout System in Django -Step-by-Step Instructions

To create a login/logout system in Django, you can follow these step-by-step instructions:


Step 1: Set Up Django Project

1. Install Django by running the command: pip install django.

2. Create a new Django project by running: django-admin startproject myproject.

3. Move into the project directory: cd myproject.

4. Create a new Django app by running: python manage.py startapp myapp.


Step 2: Configure Database

1. Open the settings.py file in your project's directory.

2. Set up the database settings according to your preferences, such as database engine, name, user, and password.

3. Run migrations to create necessary database tables by executing: python manage.py makemigrations followed by python manage.py migrate.


Step 3: Create User Authentication Views

1. Open the views.py file in your app directory.

2. Import necessary Django modules: from django.contrib.auth import authenticate, login, logout.

3. Define the login view:



from django.shortcuts import render, redirect
def login_view(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home') # Replace 'home' with the name of your home page URL
else:
return render(request, 'login.html', {'error': 'Invalid credentials.'})
else:
return render(request, 'login.html')

4. Define the logout view:



from django.shortcuts import redirect
def logout_view(request):
logout(request)
return redirect('login') # Replace 'login' with the name of your login page URL

Step 4: Create Templates

1. Create a templates directory inside your app directory.

2. Create a login.html file inside the templates directory:



<h1>Login</h1>
{% if error %}
<p>{{ error }}</p>
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<input type="text" name="username" placeholder="Username"><br>
<input type="password" name="password" placeholder="Password"><br>
<input type="submit" value="Login">
</form>

3. Create a logout.html file inside the templates directory:



<h1>Logout</h1>
<p>Are you sure you want to logout?</p>
<form method="post" action="{% url 'logout' %}">
{% csrf_token %}
<input type="submit" value="Logout">
</form>

Step 5: Define URLs

1. Open the urls.py file in your app directory.

2. Import the views: from myapp.views import login_view, logout_view.

3. Define the URLs for login and logout views:



from django.urls import path
urlpatterns = [
path('login/', login_view, name='login'),
path('logout/', logout_view, name='logout'),
]

Step 6: Update Project URLs

1. Open the urls.py file in your project's directory.

2. Import the include function: from django.urls import include.

3. Add the app's URLs to the project's URLs:



from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]


That's it! You have now set up a basic login/logout system in Django. You can customize the templates and add more functionality as per your requirements. Remember to run python manage.py runserver to start the Django development server and test your login/logout system.


Related Posts

See All

Django Todo App

Creating a Django Todo app involves several steps, including setting up the Django project, creating models for the tasks, setting up...

1 Comment


Alok Sing
Alok Sing
Jul 26

Imported logo ut

Like
bottom of page