0% found this document useful (0 votes)
42 views

Python Experiment 8 Cs Engg

This document describes a student's experiment using the Django web framework to build a web application with user login and registration functionality. The application validates user details using regular expressions. The logic for the login and signup views are defined, with the signup view checking if the password fields match, validating the username does not already exist in the database, and creating a new user if valid. The login view authenticates the user and redirects to the home page on success or displays an error on failure. The logout view logs the user out and redirects to the home page.

Uploaded by

Rahul Kumawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Python Experiment 8 Cs Engg

This document describes a student's experiment using the Django web framework to build a web application with user login and registration functionality. The application validates user details using regular expressions. The logic for the login and signup views are defined, with the signup view checking if the password fields match, validating the username does not already exist in the database, and creating a new user if valid. The login view authenticates the user and redirects to the home page on success or displays an error on failure. The logout view logs the user out and redirects to the home page.

Uploaded by

Rahul Kumawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

(ARTIFICIAL INTELLIGENCE & MACHINE LEARNING)

S.E/SEM IV/CBCGS/AIML
Academic Year: 2o21-22

Name of Student Rahul D. kumawat


Branch: CSE-AIML
Student Roll No. 23
Course Code CSL405
Course Name PYTHON PROGRAMMING
Experiment No. 8
Title of the a) To study web application using Django web
Experiment framework
to demonstrate functionality of user login
and registration
(Also validating user detail using regular
expression).
Year/Semester S.E. / IV
Date of Performance
Date of Submission
EXPERIMENT NO: 8
AIM: a) To study web application using Django web framework
to demonstrate functionality of user login and registration
(Also validating user detail using regular expression).

the logic for login and signup


from django.shortcuts import render,redirect
from django.contrib.auth.models import User
from django.contrib import auth

def signup(request):
if request.method == "POST":
if request.POST['password1'] == request.POST['password2']:
try:
User.objects.get(username = request.POST['username'])
return render (request,'accounts/signup.html', {'error':'Username is already taken!'})
except User.DoesNotExist:
user = User.objects.create_user(request.POST['username'],password=request.POST['password1'])
auth.login(request,user)
return redirect('home')
else:
return render (request,'accounts/signup.html', {'error':'Password does not match!'})
else:
return render(request,'accounts/signup.html')

def login(request):
if request.method == 'POST':
user = auth.authenticate(username=request.POST['username'],password = request.POST['password'])
if user is not None:
auth.login(request,user)
return redirect('home')
else:
return render (request,'accounts/login.html', {'error':'Username or password is incorrect!'})
else:
return render(request,'accounts/login.html')

def logout(request):
if request.method == 'POST':
auth.logout(request)
return redirect('home')

You might also like