I am having a bit of a problem. I actually wanted to create a registration form for my website which I am building using Django, and I wanted some custom fields which don't exist in the inbuild UserCreationForm in Django and I am not able to find any other way to do it, atleast not on Youtube. Any one here know how to do it
It would be a great help if any of you could help me with this I really really have to finish this website fast
Here is my code:-
models.py :-
class UserInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
full_name = models.CharField(max_length=200, null=True, blank=True)
username = models.CharField(max_length=200, null=True, blank=True)
email = models.EmailField(max_length=200, null=True, blank=True)
password = models.CharField(max_length=200,null=True, blank=True)
confirm_password = models.CharField(max_length=200, null=True, blank=True)
profile_photo = models.ImageField(null=True, blank=True, upload_to='profile_photo/',
default='profile_photo/default.jpg')
phone_number = models.CharField(max_length=10,null=True, blank=True)
address = models.TextField(null=True, blank=True)
pin_code = models.CharField(null=True, max_length=6, blank=True)
region = models.CharField(null=True, max_length=200, blank=True)
state = models.CharField(null=True, max_length=100, blank=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,
editable=False)
def __str__(self):
return str(self.username)
forms.py :-
class RegisterForm(forms.ModelForm):
class Meta:
model = UserInfo
exclude = ['user', 'created', 'id', 'profile_photo']
widgets = {
'full_name': forms.TextInput(attrs={'class':'register-textinput input'}),
'username': forms.TextInput(attrs={'class':'register-textinput1 input'}),
'email': forms.EmailInput(attrs={'class':'register-textinput2 input'}),
'phone_number': forms.TextInput(attrs={'class': 'register-textinput3 input'}),
'password': forms.PasswordInput(attrs={'class': 'password-field-textinput input', 'id': 'passwordField'}),
'confirm_password': forms.PasswordInput(attrs={'class': 'password-field-textinput input', 'id': 'cPasswordField'}),
'state': forms.TextInput(attrs={'class': 'register-textinput5 input'}),
'region': forms.TextInput(attrs={'class': 'register-textinput6 input'}),
'address': forms.TextInput(attrs={'class': 'register-textinput7 input'}),
'pin_code': forms.TextInput(attrs={'class': 'register-textinput8 input'}),
}
views.py :-
def register(request):
form = RegisterForm
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.username = user.username.lower()
user.save()
messages.success(request, "User was successfully created")
context = {'form': form}
return render(request, 'user/register.html', context)
html:-
<div>
<div class="register-container">
<div class="header-container">
<h1 class="header-text heading"><span>register</span></h1>
</div>
<div class="register-container01">
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="submit">
</form>
<div class="register-container19">
<a href="{% url 'login' %}" class="register-navlink imp">
<span>I have an account</span>
</a>
</div>
</div>
</div>