I Have Created Project Using Custom User Model & I Have Used Relationships To Get Users, But I Am Unable To Get User Email Addresses In Relationship Field
Here Is Models.py File
from django.db import models
from django.contrib.auth.models import (
BaseUserManager,
AbstractBaseUser,
PermissionsMixin,
)
# Create your models here.
class User_Manager(BaseUserManager):
use_in_migrations = True
username = None
def create_user(self, email=None, password=None, **extra_fields):
user = self.model(email=self.normalize_email(email))
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email=None, password=None, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
extra_fields.setdefault("is_active", True)
extra_fields.setdefault("is_admin", True)
if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")
return self.create_user(email, password, **extra_fields)
class User_signup(AbstractBaseUser, PermissionsMixin):
joined_on = models.DateTimeField(auto_now_add=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
FirstName = models.CharField(max_length=20)
LastName = models.CharField(max_length=20)
email = models.CharField(unique=True, max_length=100)
code = models.CharField(max_length=8)
mobile = models.CharField(max_length=13)
state = models.CharField(max_length=30)
city = models.CharField(max_length=30)
Gender = models.CharField(max_length=8)
password = models.CharField(max_length=20)
password2 = models.CharField(max_length=20)
objects = User_Manager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
class Contact_website(models.Model):
user_email = models.ForeignKey(User_signup,on_delete=models.CASCADE,null=True)
issue_date = models.DateTimeField(auto_now_add=True)
message = models.CharField(max_length=2000, null=True)
Here Is Forms.py File
from django import forms
import re
from .models import User_signup,Submit_problem,Online_survey,Contact_website
from django.core.validators import FileExtensionValidator
from django.core.exceptions import ValidationError
from django.contrib.auth.forms import AuthenticationForm
class User_signup_form(forms.ModelForm):
class Meta:
model = User_signup
fields = '__all__'
FirstName = forms.CharField(error_messages={'required': 'First Name Required'}, label='First Name', widget=forms.TextInput(
attrs={'placeholder': 'Your First Name', 'class': 'input1'}))
LastName = forms.CharField(error_messages={'required': 'Last Name Required'}, label='Last Name', widget=forms.TextInput(
attrs={'placeholder': 'Your Last Name', 'class': 'input2'}))
email = forms.EmailField(error_messages={'required': 'Email Id Required'}, label='Email Id', widget=forms.EmailInput(
attrs={'placeholder': 'Your Email ID', 'class': 'input3'}))
code = forms.CharField(disabled=True, initial='(+91)', label='Code',
widget=forms.TextInput(attrs={'class': 'countrycode'}))
mobile = forms.CharField(error_messages={'required': 'Mobile Number Required'}, label='Mobile', widget=forms.NumberInput(
attrs={'placeholder': 'Your Mobile Number', 'class': 'input4'}))
state = forms.CharField(label='State', widget=forms.Select(
choices=STATES, attrs={'class': 'select2'}))
city = forms.CharField(label='City', widget=forms.Select(
choices=CITY, attrs={'class': 'select3'}))
Gender = forms.CharField(error_messages={'required': 'Select Gender'}, label='Gender :- ',
widget=forms.RadioSelect(choices=CHOICES, attrs={'class': 'radio1'}))
password = forms.CharField(error_messages={'required': 'Choose Password'}, label='Password', widget=forms.PasswordInput(
attrs={'placeholder': 'Choose Password', 'class': 'input5'}))
password2 = forms.CharField(error_messages={'required': 'Reenter Password Agian'}, label='Reenter Password', widget=forms.PasswordInput(
attrs={'placeholder': 'Choose Same Password Again', 'class': 'input6'}))
def clean_FirstName(self):
cleaned_data = super(User_signup_form, self).clean()
valFname = cleaned_data.get('FirstName')
valpat = '[0-9\~\`\!\@\#\$\%\^\&\*\(\)\-\_\|\[\]{\}\;\:\"\,\.\/\?\=\+\<\>]'
if len(valFname) < 3:
raise forms.ValidationError('Minimum 3 Character Required')
if (re.search(valpat, valFname)):
raise forms.ValidationError('Numbers & Symbols Not Allowed')
return valFname
def clean_LastName(self):
cleaned_data = super(User_signup_form, self).clean()
vallname = cleaned_data.get('LastName')
valFname = cleaned_data.get('FirstName')
valpat1 = '[0-9\~\`\!\@\#\$\%\^\&\*\(\)\-\_\|\[\]{\}\;\:\"\,\.\/\?\=\+\<\>]'
if (re.search(valpat1, vallname)):
raise forms.ValidationError('Numbers & Symbols Not Allowed')
if vallname == valFname:
raise forms.ValidationError("First & Last Name Should'nt Be Same")
return vallname
def clean_mobile(self):
cleaned_data = super(User_signup_form, self).clean()
mobile = cleaned_data.get('mobile')
regex3 = '[~\`\!\@\#\$\%\^\&\*\(\)\-\_\|\[\]{\}\;\:\"\,\.\/\?\=\+\<\>\']'
userfilter = User_signup.objects.filter(mobile=mobile)
if userfilter:
raise forms.ValidationError('Mobile Already Registered')
if len(mobile) < 8:
raise forms.ValidationError('Mobile Too Short')
if len(mobile) > 13:
raise forms.ValidationError('Mobile Too Long')
if (re.search(regex3, mobile)):
raise forms.ValidationError('Alphabets & Symbols Not Allowed')
return mobile
def clean_email(self):
cleaned_data = super(User_signup_form, self).clean()
username = cleaned_data.get('email')
userfilter = User_signup.objects.filter(email=username)
if userfilter:
raise forms.ValidationError('Email Already Registered')
return username
def clean_password(self):
cleaned_data = super(User_signup_form, self).clean()
password = cleaned_data.get('password')
regex1 = '[0-9]'
regex2 = '[A-Z]'
regex3 = '[a-z]'
regex4 = '[~\`\!\@\#\$\%\^\&\*\(\)\-\_\|\[\]{\}\;\:\"\,\.\/\?\=\+\<\>]'
if len(password) < 8:
raise forms.ValidationError('Password Must Contain 8 Characters')
if not (re.search(regex1, password)):
raise forms.ValidationError(
'Password Must Contain 1 Digit Between[0-9]')
if not (re.search(regex2, password)):
raise forms.ValidationError(
'Password Must Have 1 Uppercase Between[A-Z]')
if not (re.search(regex3, password)):
raise forms.ValidationError(
'Password Must Have 1 Lowercase Between[a-z]')
if not (re.search(regex4, password)):
raise forms.ValidationError(
'Password Must Have 1 Symbol Eg.[@,$,%,&,*]')
return password
def clean_rpassword(self):
cleaned_data = super(User_signup_form, self).clean()
password = cleaned_data.get('password')
rpassword = cleaned_data.get('rpassword')
if rpassword != password:
raise forms.ValidationError('Password Does Not Match')
return rpassword
def clean_state(self):
cleaned_data = super(User_signup_form, self).clean()
state = cleaned_data.get('state')
valstn = 'Select'
if state == valstn:
raise forms.ValidationError('Select State')
return state
def clean_city(self):
cleaned_data = super(User_signup_form, self).clean()
city = cleaned_data.get('city')
valct = 'Select'
if city == valct:
raise forms.ValidationError('Select City')
return city
class Contact_website_form(forms.ModelForm):
class Meta:
model = Contact_website
fields = ['message']
message = forms.CharField(
max_length=10000,
error_messages={'required': 'Enter Your Message'},
widget=forms.Textarea(
attrs={
'class': 'contact_website_message',
'placeholder': 'Enter Your Message Here'
}),
label='Enter Your Message')
Here Is Views.py File
def userregistration(request):
if not request.user.is_authenticated:
if request.method == 'POST':
fm = User_signup_form(request.POST)
if fm.is_valid():
firstName = fm.cleaned_data.get('FirstName')
lastname = fm.cleaned_data.get('LastName')
email = fm.cleaned_data.get('email')
confirm_email = email.lower()
code = fm.cleaned_data.get('code')
mobile = fm.cleaned_data.get('mobile')
state = fm.cleaned_data.get('state')
city = fm.cleaned_data.get('city')
gender = fm.cleaned_data.get('Gender')
password = make_password(fm.cleaned_data.get('password'))
password2 = make_password(fm.cleaned_data.get('password2'))
user1 = User_signup(
FirstName=firstName,
LastName=lastname,
email=confirm_email,
code=code,
mobile=mobile,
state=state,
city=city,
Gender=gender,
password=password,
password2=password2,
)
user1.save()
return redirect('success')
else:
fm = User_signup_form()
if request.user.is_authenticated:
return redirect('/submitproblemafterlogin/')
return render(request, 'jobglobel/usersignup.html',{'form': fm})
@login_required(login_url='/userlogin')
def contact_website_view(request):
if request.method == 'POST':
fm = Contact_website_form(request.POST)
if fm.is_valid():
message = fm.cleaned_data.get('message')
user1 = Contact_website(
message=message,
)
user1.save()
return redirect('aftercontactingwebsite')
else:
fm = Contact_website_form()
return render(request, 'jobglobel/contact_website.html', {'form': fm})