Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #models.py
- from django.db import models
- from django.contrib.auth.models import User
- from django.db.models.signals import post_save
- from django.conf import settings
- # Create your models here.
- class UserProfile(models.Model):
- CLG_CHOICES = (
- ('MIT', 'Mahakal Institute Of Technology'),
- ('MITM', 'Mahakal Institute Of Technology And Management'),
- )
- BRANCH_CHOICES = (
- ('CS', 'Computer Science And Engineering'),
- ('IT', 'Information Technology'),
- ('CE', 'Civil Engineering'),
- ('ME', 'Mechanical Engineering'),
- ('EC', 'Electronic Engineering'),
- )
- college = models.CharField(max_length=5, choices=CLG_CHOICES, blank=False)
- branch = models.CharField(max_length=2, choices=BRANCH_CHOICES, blank=False)
- roll_number = models.CharField(max_length=12,verbose_name='Roll Number',unique=True)
- dob = models.DateField(blank=True,null=True,verbose_name='Date Of Birth')
- user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='user')
- def __str__(self):
- return 'Welcome {}'.format(self.roll_number)
- #forms.py
- from .models import *
- from django import forms
- from django.contrib.auth.models import User
- #User registration
- class UserRegistrationForm(forms.ModelForm):
- password = forms.CharField(label='Password', widget=forms.PasswordInput)
- password2 = forms.CharField(label='Repeat Password', widget=forms.PasswordInput)
- class Meta:
- model = User
- fields = ('username','email',)
- def clean_password2(self):
- cd = self.cleaned_data
- if cd['password'] != cd['password2']:
- raise forms.ValidationError("password don't match.")
- return cd['password2']
- class UserProfile(forms.ModelForm):
- class Meta:
- model = UserProfile
- fields = ('college','branch','roll_number','dob')
- #views.py
- from django.shortcuts import render
- from .forms import *
- # Create your views here.
- def index(request):
- return render(request,'register/index.html')
- def register(request):
- if request.method == 'POST':
- user_form = UserRegistrationForm(request.POST)
- user_profile = UserProfile(request.POST)
- if user_form.is_valid() and user_profile.is_valid():
- #create new user object to avaoid saving it
- new_user = user_form.save(commit=False)
- new_profile = user_profile.save(commit=False)
- # set the choosen password
- new_user.set_password(user_form.cleaned_data['password'])
- #profile = UserProfile.objects.create(user=new_user)
- new_user.save()
- new_profile.save()
- return render(request, 'register/register_done.html', {'new_user':new_user})
- else:
- user_form = UserRegistrationForm()
- user_profile = UserProfile()
- return render(request, 'register/register.html',{'user_form':user_form,'user_profile':user_profile})
Add Comment
Please, Sign In to add comment