how to return a verification error message when a user is already authenticated with that same Email address or when the email already exists

Viewed 8

i need help on how to return a validation error message when a user try's to register with the already registered email. below its my code

views.py

from audioop import reverse
from email import policy
from multiprocessing import context
from urllib import response

from django.urls import is_valid_path
from .models import actiongame, feature, gpost 
from django.contrib.auth import login as dj_login , authenticate, login as real_login, login as bond_to_terms
from django.shortcuts import render
from django.contrib.auth.models import User
from .forms import   ProfileUpdateForm, SignUpForm, LoginForm, UserUpdateForm, UserTermsAggrement
from django.shortcuts import redirect
from django.contrib import messages 




def trials(request):
  # if this is a POST request we need to process the form data
  template = 'trials.html'
  if request.method == 'POST':
      email = request.POST.get('email')
      form = SignUpForm(request.POST)
      if form.is_valid():
          if User.objects.filter(email=email).exists():
              messages.info(request, 'Email Aready Used')
              form.save()
          username = form.cleaned_data.get('username')
          raw_password = form.cleaned_data.get('password1')
          email = form.cleaned_data.get('email')
          
          user = authenticate(username=username, password=raw_password, email=email)
          dj_login(request,user)
          return redirect('signup:terms')
  else:
      form = SignUpForm()
  return render(request, 'trials.html', {'form': form})

form.py


from pyexpat import model
from .models import profile as ProfileModel
from dataclasses import fields
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class SignUpForm(UserCreationForm):
    first_name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}),help_text='*First Name is Required', required=True)
    last_name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}),required=False,help_text='*Optional')
    date_of_birth = forms.DateField(widget=forms.DateInput(attrs={'class':'form-control'}),help_text='*Enter A Valid Date Of Birth',required=True)
    email = forms.EmailField(widget=forms.EmailInput(attrs={'class':'form-control'}), required=True,help_text='*Enter Email Required', )
    username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}), required=True, help_text='*Username is Required')
    password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control'}))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control'}),help_text='*First password must match with the Second Password')
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')

trials.html

{% block content %}
<h2>Signup</h2>
<form method="post">
    {% csrf_token %}
    {% for field in form %}
    <p>
        {{ field.label_tag }} <br>
        {{ field }} <br>
    {% if field.help_text %}
    <p class="help_text">{{ field.help_text }}</p>
    {% endif %}
    {% for error in field.errors %}
    <p class="error">{{ error }}</p>
    {% endfor %}
    </p>
    {% endfor %}
    <input type="submit" value="Register" class="register">
</form>

{% endblock %}

PLEASE I NEED HELP ON SOME OF THESE QUESTIONS BELOW:

  • How can l write that validation error in django validators.py?
  • Where did i made a mistake aand how to solve it
0 Answers
Related