যেহেতু authentication form গুলা জ্যাংগো আমাদের বাই ডিফল্ট দিয়ে দেয় সুতরাং আমাদের models.py ফাইলের মধ্যে কোন রকম কাজ করার প্রয়োজন নেই। বরং আমরা forms.py ফাইলের মধ্যে নিচের মত করে প্রত্যেকটার জন্য কাজ করবঃ
forms.py
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django import forms
class RegisterForm(UserCreationForm):
first_name = forms.CharField(widget=forms.TextInput(attrs={'id':'required'}))
class Meta:
model = User
fields = ['username','first_name','last_name','email']
class userDataChangeForm(UserChangeForm):
password = None
class Meta:
model = User
fields = ['first_name','last_name','email']
এবার লগ ইন, লগ আউট, চেঞ্জ পাসওয়ার্ড, চেঞ্জ ইউজার ডাটা করার জন্য views.py ফাইলের মধ্যে নিচের মত করে প্রত্যেকটার জন্য কাজ করিঃ
views.py
from django.shortcuts import render,redirect
from authentication_app.forms import RegisterForm,userDataChangeForm
from django.contrib import messages
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm, SetPasswordForm
from django.contrib.auth import authenticate,login,logout, update_session_auth_hash
# Create your views here.
def signup(request):
if not request.user.is_authenticated:
if request.method == 'POST':
register_form = RegisterForm(request.POST)
if register_form.is_valid():
messages.success(request,'Account created successfully')
register_form.save()
print(register_form.cleaned_data)
return redirect('login')
else:
register_form = RegisterForm()
return render(request,'signup.html',{'form':register_form})
else:
return redirect('profile')
def login_form(request):
if not request.user.is_authenticated:
if request.method == 'POST':
login_form = AuthenticationForm(request=request, data=request.POST)
if login_form.is_valid():
userName = login_form.cleaned_data['username']
userPass = login_form.cleaned_data['password']
user = authenticate(username=userName, password=userPass)
if user is not None:
login(request,user)
return redirect('profile')
else:
login_form = AuthenticationForm()
return render(request,'login.html', {'form':login_form})
else:
return redirect('profile')
def profile(request):
if request.user.is_authenticated:
return render(request,'profile.html',{'user':request.user})
else:
return redirect('login')
def user_logout(request):
logout(request)
return redirect('login')
def change_pass(request):
if request.user.is_authenticated:
if request.method == 'POST':
change_form = PasswordChangeForm(user=request.user, data = request.POST)
if change_form.is_valid():
change_form.save()
update_session_auth_hash(request,change_form.user)
return redirect('profile')
else:
change_form = PasswordChangeForm(user = request.user)
return render(request,'change_pass.html', {'form':change_form})
else:
return redirect('login')
def change_pass_without_old_pass(request):
if request.user.is_authenticated:
if request.method == 'POST':
change_form = SetPasswordForm(user=request.user, data = request.POST)
if change_form.is_valid():
change_form.save()
update_session_auth_hash(request,change_form.user)
return redirect('profile')
else:
change_form = SetPasswordForm(user = request.user)
return render(request,'change_pass.html', {'form':change_form})
else:
return redirect('login')
def change_user_data(request):
if request.user.is_authenticated:
if request.method == 'POST':
register_form = userDataChangeForm(request.POST,instance=request.user)
if register_form.is_valid():
messages.success(request,'User Data successfully Changed')
register_form.save()
print(register_form.cleaned_data)
return redirect('profile')
else:
register_form = userDataChangeForm(instance=request.user)
return render(request,'change_user_data.html',{'form':register_form})
else:
return redirect('signup')
Post a Comment