How to create Register page using Django and Python coding

  Python

Email

views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpRequest
from django.contrib.auth import authenticate, login,logout
from django.contrib.auth.decorators import login_required
from .forms import Reigsterfrom
from django.contrib.auth.models import User

 

def user_register(request):
template = ‘login/register.html’
if request.method ==’POST’:
form = Reigsterfrom(request.POST)
if form.is_valid():
if User.objects.filter(email=form.cleaned_data[’email’]).exists():
return render(request, template, {
‘form’: form,
‘error_message’: ‘Email already exists.’
})
elif form.cleaned_data[‘password’] != form.cleaned_data[‘cmpassword’]:
return render(request, template, {
‘form’: form,
‘error_message’: ‘Passwords do not match.’
})
else:
user = User.objects.create_user(
form.cleaned_data[’email’],
form.cleaned_data[‘password’]
)
user.email = form.cleaned_data[’email’]
user.password = form.cleaned_data[‘password’]
user.cmpassword = form.cleaned_data[‘cmpassword’]
user.save()
login(request, user)
return HttpResponseRedirect(‘register’)
else:
form = Reigsterfrom()

 

return render(request, template, {‘form’: form})
models.py
from django.db import models
from datetime import date

 

class Account_create(models.Model):
email = models.CharField(max_length=30)
password = models.CharField(max_length=50)
cmpassword = models.CharField(max_length=30)

urls.py

from django.urls import path
from.import views

 

urlpatterns = [
path(‘register/’,views.user_register, name=’register’),
]
Spread the Code

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of