Skip to main content

User Authentication in Django (Login, Register, Logout)

 User Authentication in Django (Login, Register, Logout)


๐Ÿง  Introduction

Modern web applications lo User Authentication chala important concept.
Simple ga cheppali ante, website lo users ni identify cheyadam, secure access ivvadam.

Manam daily use chese apps like:

  • Social media
  • E-commerce websites
  • Blogging platforms

anni authentication system meeda depend ayi untayi.


๐Ÿ” What is User Authentication?

User Authentication ante:

๐Ÿ‘‰ User evaro verify cheyadam
๐Ÿ‘‰ Valid credentials (username, password) check cheyadam
๐Ÿ‘‰ Correct ayite access ivvadam

Idi security backbone ani cheppachu.


⚙️ Main Components

1️⃣ Register (Sign Up)

Kotha user account create chesukuntadu.

๐Ÿ‘‰ User details:

  • Username
  • Email
  • Password

Store avutayi database lo.


2️⃣ Login

Already account unna user login avutadu.

๐Ÿ‘‰ System:

  • Username/password verify chestundi
  • Correct ayite login allow chestundi

3️⃣ Logout

User session ni close cheyadam.

๐Ÿ‘‰ Security kosam:

  • Logout taruvata restricted pages access avvu

๐Ÿ—️ How Django Handles Authentication

Django lo built-in ga powerful authentication system undi.

๐Ÿ‘‰ Features:

  • User model ready ga untundi
  • Password hashing automatic
  • Login/logout handling easy

Manam scratch nundi build cheyyalsina avasaram ledu — Django already provide chestundi.


๐Ÿ”„ Authentication Flow

Simple flow ila untundi:

  1. User register chestadu
  2. Credentials database lo store avutayi
  3. User login attempt chestadu
  4. System verify chestundi
  5. Valid ayite session create avutundi
  6. Logout ayite session destroy avutundi

๐Ÿ’ก Why Authentication is Important?

๐Ÿ‘‰ Unauthorized access prevent cheyadaniki
๐Ÿ‘‰ User-specific data protect cheyadaniki
๐Ÿ‘‰ Personalized experience ivvadam kosam
๐Ÿ‘‰ Secure web application build cheyadaniki


๐Ÿš€ Real-Time Use Case (Mana Blog Project)

Mana Django blog project lo:

๐Ÿ‘‰ Only logged-in users:

  • Blog create cheyali
  • Edit cheyali
  • Delete cheyali

๐Ÿ‘‰ Public users:

  • Blogs chudachu (read-only)

๐Ÿ”ฅ Conclusion

User Authentication ane di every web application ki must-have feature.

Django tho:

  • Easy implementation
  • Secure system
  • Scalable architecture

Next step lo manam:

๐Ÿ‘‰ Full Login, Register, Logout code build cheddam
๐Ÿ‘‰ Blog ni user-based system ga marchadam ๐Ÿš€


๐Ÿ“ข Call To Action

“Code cheyyadam tho ne nerchukovali ๐Ÿ’ป๐Ÿ”ฅ”


views.py

from django.shortcuts import render, redirect
from django.contrib.auth import login, logout
from .forms import SignupForm, LoginForm

# SIGNUP
def signup_view(request):
    form = SignupForm()

    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(form.cleaned_data['password'])
            user.save()
            return redirect('login')

    return render(request, 'signup.html', {'form': form})


# LOGIN
def login_view(request):
    form = LoginForm()

    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            login(request, form.user)
            return redirect('dashboard')

    return render(request, 'login.html', {'form': form})


# LOGOUT
def logout_view(request):
    logout(request)
    return redirect('login')



๐ŸŒ urls.py

from django.urls import path
from . import views

urlpatterns = [
path('signup/', views.signup_view, name='signup'),
path('login/', views.login_view, name='login'),
path('logout/', views.logout_view, name='logout'),
]


๐ŸŽจ HTML Templates

๐Ÿงฉ base.html

<!DOCTYPE html>
<html>
<head>
    <title>Auth System</title>
</head>
<body>

<nav>
    {% if user.is_authenticated %}
        <p>Welcome {{ user.username }}</p>
        <a href="{% url 'logout' %}">Logout</a>
    {% else %}
        <a href="{% url 'login' %}">Login</a>
        <a href="{% url 'signup' %}">Signup</a>
    {% endif %}
</nav>

<hr>

{% block content %}{% endblock %}

</body>
</html>

๐Ÿ“ signup.html


{% extends 'base.html' %}

{% block content %}
<h2>Signup</h2>

<form method="POST">
    {% csrf_token %}

    <input type="text" name="username" placeholder="Username"><br><br>
    <input type="email" name="email" placeholder="Email"><br><br>
    <input type="password" name="password" placeholder="Password"><br><br>
    <input type="password" name="confirm_password" placeholder="Confirm Password"><br><br>

    <button type="submit">Signup</button>
</form>

<p>Already have account? <a href="{% url 'login' %}">Login</a></p>

{% if form.errors %}
    <p style="color:red;">{{ form.errors }}</p>
{% endif %}
{% endblock %}


๐Ÿ” login.html


{% extends 'base.html' %}

{% block content %}
<h2>Login</h2>

<form method="POST">
    {% csrf_token %}

    <input type="text" name="username" placeholder="Username"><br><br>
    <input type="password" name="password" placeholder="Password"><br><br>

    <button type="submit">Login</button>
</form>

<p>Don't have account? <a href="{% url 'signup' %}">Signup</a></p>

{% if form.errors %}
    <p style="color:red;">Invalid login details</p>
{% endif %}
{% endblock %}


๐Ÿ“Š dashboard.html


{% extends 'base.html' %}

{% block content %}
<h2>Dashboard</h2>

<p>Welcome {{ user.username }} ๐ŸŽ‰</p>
<p>You are successfully logged in ๐Ÿ”</p>
{% endblock %}


๐Ÿ”’ (Protect Dashboard)


from django.contrib.auth.decorators import login_required

@login_required
def dashboard(request):
    return render(request, 'dashboard.html')

Comments

Popular posts from this blog

Django ante enti? (Beginner Friendly Telugu Guide)

  Introduction Meeru web development start cheyyali anukuntunnara? Leka backend development nerchukovali anukuntunnara? Appudu meeru tappakunda vinnadi oka peru ๐Ÿ‘‰ Django Ee article lo manam simple ga, clear ga ardham ayyela chuddam — Django ante enti, enduku use chestaru, ela start cheyyali.

Python Day 5 – If Else Conditions

  ๐Ÿ Python Day 5 – If Else Conditions ๐Ÿง  Introduction ๐Ÿ Python Day 5 – If Else Conditions Ippati varaku manam operators nerchukunnam. I roju manam decision making (If-Else) nerchukundam. ๐Ÿ‘‰ Program lo condition base chesi decisions tiskovadaniki use avutundi ๐Ÿ’ก Example: Age 18 kanna ekkuva unte → eligible Lekapothe → not eligible ๐Ÿ”น Basic If Condition age = 20 if age > 18 : print ( "Eligible" ) ๐Ÿ”น If-Else Condition age = 16 if age >= 18 : print ( "Eligible" ) else : print ( "Not Eligible" ) ๐Ÿ”น If-Elif-Else marks = 75 if marks >= 90 : print ( "Grade A" ) elif marks >= 60 : print ( "Grade B" ) else : print ( "Grade C" ) ๐Ÿงช Real Example (User Input) age = int ( input ( "Enter your age: " )) if age >= 18 : print ( "You can vote ✅" ) else : print ( "You cannot vote ❌" ) ⚠️ Important Points ๐Ÿ‘‰ Indentation (space) very import...

Django Templates & HTML Rendering (Complete Guide Telugu)

  Introduction Previous article lo manam Views & URLs chusam. Ipudu next important concept ๐Ÿ‘‰ Templates & HTML Rendering Django lo dynamic HTML pages ela create cheyyalo ee article lo nerchukundam ๐Ÿ”ฅ ๐Ÿง  Templates ante enti? ๐Ÿ‘‰ Template ante HTML file "T emplate ante HTML file matrame kaadu, Django lo dynamic content ni display cheyadaniki use chestam. Example ga, database lo unna blog posts ni HTML page lo chupinchadaniki templates use avutayi. " But special enti ante: ๐Ÿ‘‰ Dynamic data display cheyyachu (Python data → HTML lo show) ๐Ÿ“ Templates folder create cheyyadam Mee app lo templates ane folder create cheyandi: myapp/ templates/ index.html ⚙️ settings.py lo config cheyyali settings.py open cheyandi: TEMPLATES = [ { 'DIRS': [], ... }, ] ๐Ÿ‘‰ Usually app templates automatic ga detect chestundi ๐Ÿ’ป Simple HTML Template index.html : <!DOCTYPE html> <html> <head> <title>Django Page</title> ...