pmalviya13

Untitled

Dec 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. #models.py
  2.  
  3. from django.db import models
  4. from django.contrib.auth.models import User
  5. from django.db.models.signals import post_save
  6. from django.conf import settings
  7.  
  8. # Create your models here.
  9. class UserProfile(models.Model):
  10.     CLG_CHOICES = (
  11.         ('MIT', 'Mahakal Institute Of Technology'),
  12.         ('MITM', 'Mahakal Institute Of Technology And Management'),
  13.     )
  14.     BRANCH_CHOICES = (
  15.         ('CS', 'Computer Science And Engineering'),
  16.         ('IT', 'Information Technology'),
  17.         ('CE', 'Civil Engineering'),
  18.         ('ME', 'Mechanical Engineering'),
  19.         ('EC', 'Electronic Engineering'),
  20.     )
  21.  
  22.     college = models.CharField(max_length=5, choices=CLG_CHOICES, blank=False)
  23.     branch = models.CharField(max_length=2, choices=BRANCH_CHOICES, blank=False)
  24.     roll_number = models.CharField(max_length=12,verbose_name='Roll Number',unique=True)
  25.     dob = models.DateField(blank=True,null=True,verbose_name='Date Of Birth')
  26.     user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='user')
  27.     def __str__(self):
  28.         return 'Welcome {}'.format(self.roll_number)
  29.  
  30. #forms.py
  31.  
  32. from .models import *
  33. from django import forms
  34. from django.contrib.auth.models import User
  35.  
  36. #User registration
  37. class  UserRegistrationForm(forms.ModelForm):
  38.     password = forms.CharField(label='Password', widget=forms.PasswordInput)
  39.     password2 = forms.CharField(label='Repeat Password', widget=forms.PasswordInput)
  40.  
  41.     class Meta:
  42.         model = User
  43.         fields = ('username','email',)
  44.     def clean_password2(self):
  45.         cd = self.cleaned_data
  46.         if cd['password'] != cd['password2']:
  47.             raise forms.ValidationError("password don't match.")
  48.         return cd['password2']
  49.  
  50. class UserProfile(forms.ModelForm):
  51.     class Meta:
  52.         model = UserProfile
  53.         fields = ('college','branch','roll_number','dob')
  54.  
  55. #views.py
  56.  
  57. from django.shortcuts import render
  58. from .forms import *
  59.  
  60. # Create your views here.
  61.  
  62. def index(request):
  63.     return render(request,'register/index.html')
  64. def register(request):
  65.     if request.method == 'POST':
  66.         user_form = UserRegistrationForm(request.POST)
  67.         user_profile = UserProfile(request.POST)
  68.         if user_form.is_valid() and user_profile.is_valid():
  69.             #create new user object to avaoid saving it
  70.             new_user = user_form.save(commit=False)
  71.             new_profile = user_profile.save(commit=False)
  72.             # set the choosen password
  73.             new_user.set_password(user_form.cleaned_data['password'])
  74.             #profile = UserProfile.objects.create(user=new_user)
  75.             new_user.save()
  76.             new_profile.save()
  77.             return render(request, 'register/register_done.html', {'new_user':new_user})
  78.     else:
  79.         user_form = UserRegistrationForm()
  80.         user_profile = UserProfile()
  81.     return render(request, 'register/register.html',{'user_form':user_form,'user_profile':user_profile})
Add Comment
Please, Sign In to add comment