hello there
need your help
i have wrote some codes to register my user by sending random code and confirm the code in seperate form and template
after all i have done i dont know why after complete and send the form the next template does’nt show up
here are my view , model and the url i got in my browser url bar
what do you think?
view.py
from django.shortcuts import render , redirect
from django.views import View
from .forms import UserRegistrationForm , VerifyCodeForm
import random
from utils import send_otp_code
from .models import OtpCode , User
from django.contrib import messages
class UserRegisterView(View):
form_class = UserRegistrationForm
def get(self , request):
form = self.form_class
return render(request , 'accounts/register.html' , {'form':form})
def post(self , request):
form = self.form_class(request.POST)
if form.is_valid():
random_code = random.randint(1000 , 9999)
send_otp_code(phone_number= form.cleaned_data['phone_number'] , code=random_code)
OtpCode.objects.create(phone_number = form.cleaned_data['phone_number'] , code= random_code)
request.session['user_registration_info'] = {
'phone_number':form.cleaned_data['phone_number'],
'full_name':form.cleaned_data['full_name'],
'email':form.cleaned_data['email'],
'password':form.cleaned_data['password']
}
messages.success(request , 'we sent you a code !' , 'success')
return redirect('accounts:verify_code')
return redirect('home:home')
class UserRegistrationVerifyCodeView(View):
from_class = VerifyCodeForm
def get(self , request):
form = self.from_class
return render(request , 'accounts/verify.html' , {'form':form})
def post(self , request):
user_session = request.session['user_registration_info']
code_instance = OtpCode.objects.get(phone_number =user_session['phone_number'])
form = self.from_class(request.POST)
if form.is_valid():
cd = form.cleaned_data
if cd['code']==code_instance.code:
User.objects.create_user(user_session['email'] , user_session['full_name'] ,
user_session['phone_number'] , user_session['password'])
code_instance.delete()
messages.success(request , 'you registred' , 'success')
return redirect('home:home')
else:
messages.error(request , 'code doesnt match' , 'danger')
return redirect('accounts:verify_code')
return redirect('home:home')
model.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from .managers import UserManager
class User(AbstractBaseUser):
email = models.EmailField(max_length=255 , unique=True)
full_name = models.CharField(max_length=100)
phone_number = models.CharField(max_length=11 , unique=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'phone_number'
REQUIRED_FIELDS = ['email' , 'full_name']
def __str__(self) -> str:
return self.email
def has_perm(self , perm , obj=None):
return True
def has_module_perms(self , app_label):
return True
def is_staff(self):
return self.is_admin
class OtpCode(models.Model):
phone_number = models.CharField(max_length=11)
code = models.PositiveSmallIntegerField()
created = models.DateTimeField(auto_now=True)
def __str__(self) -> str:
return f'{self.phone_number} - {self.code} - {self.created}'
the url i get instead of redirecting to next template
[type or paste code here](http://127.0.0.1:8000/accounts/register/?csrfmiddlewaretoken=BQI2MOXOsD4UYY7JlnfEHdEMFPZG2i8MaJ9L3b6lzXeZXoLTBzM5FdnLYrdCIlXW&email=leon%40email.com&phone_number=12345678910&full_name=leon+k+&password=leon)