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.

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> ...

Django Views & URLs (Complete Guide Telugu)

Introduction Previous article lo manam Django project create chesam. Ipudu chuddam 👉 user request vachinappudu Django ela respond avthundi? Django lo Views & URLs anevi chala important concepts 🔥 🧠 Simple ga ardham: 👉 URL = address 👉 View = action (logic) User URL enter chesthe → View execute avthundi 🔗 URLs ante enti? Example: http://127.0.0.1:8000/about/ http://127.0.0.1:8000/contact/ 👉 /about/ , /contact/ ni URLs antaru Django lo ee URLs ni urls.py file lo define chestam ⚙️ urls.py Example from django.urls import path from . import views urlpatterns = [ path('about/', views.about_view), ] 👉 Ikkada: 'about/' → URL about_view → View function ⚡ Views ante enti? 👉 View ante Python function User request vachinappudu: 👉 View function run avthundi 👉 Response return chestundi 💻 Simple View Example from django.http import HttpResponse def about_view(request): return HttpResponse("Hello from About Page") 👉 Browser lo /about/ open ches...