I have a model ApplicantRegister which has various fields. I want to register applicant after fill up the required information. After Registration and Mail Verification I want to logged in applicant through email and password. But I am getting Log in Details Wrong even though i submitted right information. Can't we use django built in authenticate method in custom model ?
models.py
class ApplicantRegister(models.Model):
applicant_name_in_english = models.CharField(max_length=50)
applicant_name_in_bengali = models.CharField(max_length=50)
father_name_in_english = models.CharField(max_length=50)
father_name_in_bengali = models.CharField(max_length=50)
mother_name_in_english = models.CharField(max_length=50)
mother_name_in_bengali = models.CharField(max_length=50)
marital_status = models.CharField(max_length=20)
spouse_name_in_english = models.CharField(max_length=50, blank=True)
spouse_name_in_bengali = models.CharField(max_length=50, blank=True)
gender = models.CharField(max_length=10, choices=GENDER_CHOICES)
present_address_in_english = models.TextField()
present_address_in_bengali = models.TextField()
permanent_address_in_english = models.TextField()
permanent_address_in_bengali = models.TextField()
religion = models.CharField(max_length=20)
home_district = models.CharField(max_length=30,
choices=DISTRICT_CHOICES)
national_id = models.CharField(max_length=30)
date_of_birth = models.DateField()
phone = models.CharField(max_length=50)
confirm_phone = models.CharField(max_length=50)
email = models.EmailField(unique=True)
confirm_email = models.EmailField()
password = models.CharField(max_length=50)
confirm_password = models.CharField(max_length=50)
applicant_photo = models.ImageField(upload_to='applicantImages/',
validators=[validate_file_size([200]),
validate_file_extension, validate_image_dimension([300],
[300])])
applicant_signature = \
models.ImageField(upload_to='applicantSignature/',
validators=[validate_file_size([100]),
validate_file_extension,
validate_image_dimension([300], [80])])
reference_1 = models.TextField()
reference_2 = models.TextField()
is_active = models.BooleanField(default=False)
def __str__(self):
return self.applicant_name_in_english
views.py
def registerView(request):
applicant_form = ApplicantRegisterModelForm()
if request.method == 'POST':
applicant_form = ApplicantRegisterModelForm(request.POST, request.FILES)
if applicant_form.is_valid():
applicant = applicant_form.save(commit=False)
applicant.is_active = False
applicant.save()
current_site_info = get_current_site(request)
mail_subject = 'The Activation link has been sent to your email address, Please Activate Your Account'
message = render_to_string('registration_app/acc_active_email.html', {
'user': applicant,
'domain': current_site_info.domain,
'uid':urlsafe_base64_encode(force_bytes(applicant.pk)),
'token':account_activation_token.make_token(applicant),
})
to_email = applicant_form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
print('email sent')
return HttpResponse('Please proceed confirm your email address to complete the registration')
else:
print('Errors in data')
print(applicant_form.errors)
data = {
'applicant_form':applicant_form,
}
return render(request, 'registration_app/register.html', context=data)
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
applicant = ApplicantRegister.objects.get(pk=uid)
print(uid, applicant)
except(TypeError, ValueError, OverflowError, ApplicantRegister.DoesNotExist):
applicant = None
if applicant is not None and account_activation_token.check_token(applicant, token):
applicant.is_active = True
applicant.save()
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
def login_page(request):
return render(request, 'registration_app/login.html', context={})
def applicant_login(request):
if request.method == 'POST':
email = request.POST.get('email')
# username = request.POST.get('username')
password = request.POST.get('password')
# print(username, password)
applicant = authenticate(username=email, password=password)
print(applicant)
if applicant:
if applicant.is_active:
login(request, applicant)
# To change the urlpatterns we are using this method
return HttpResponseRedirect(reverse('home_page_app:home'))
# if we return using render function then urlpatterns does not change that's why we need to use previous method
# return render(request, 'registration_app/index.html', context={})
# this method also does not change urlpatterns
# return index(request)
else:
return HttpResponse('Account is not active')
else:
return HttpResponse('Log In details are wrong')
else:
# return render(request, 'registration_app/login.html', context={})
return HttpResponseRedirect(reverse('registration_app:login'))
@login_required
def applicant_logout(request):
logout(request)
return HttpResponseRedirect(reverse('home_page_app:home'))
login.html
<form action="{% url 'registration_app:applicant-login' %}" method="POST">
{% csrf_token %}
<label for="email">Email:</label>
<input type="email" name="email" id="id_email" placeholder="Enter your email" required>
<label for="password">Password:</label>
<input type="password" name="password" id="id_password" placeholder="Enter Your Password" required>
<br>
<input type="submit" value="Log In">
</form>
CodePudding user response:
It is possible to extend and customize the base user model for authentication but you have to follow the Django documentation Extending the existing User model Substituting a custom User model Using a custom user model when starting a project
These three link should help you.