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.dispatch import receiver
- # Create your models here.
- class Profile(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)
- branch = models.CharField(max_length=5,choices=BRANCH_CHOICES)
- birth_date = models.DateField(blank=True, null=True)
- user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='profile')
- def __str__(self):
- return "profile of {}".format(self.user)
- @receiver(post_save, sender=User)
- def create_user_profile(sender,instance,created,**kwargs):
- if created:
- Profile.objects.create(user=instance)
- @receiver(post_save,sender=User)
- def save_user_profile(sender,instance,**kwargs):
- instance.profile.save()
- #forms.py
- from django import forms
- from django.contrib.auth.models import User
- from django.contrib.auth.forms import UserCreationForm
- from .models import *
- class SignUpForm(forms.ModelForm):
- 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 = forms.ChoiceField(choices=CLG_CHOICES)
- branch = forms.ChoiceField(choices=BRANCH_CHOICES)
- birth_date = forms.DateField()
- password = forms.CharField(label='Password', widget=forms.PasswordInput)
- password2 = forms.CharField(label='Repeat Password', widget=forms.PasswordInput)
- class Meta:
- model = User
- fields= ('college','branch','username','email','birth_date','password','password2')
- def clean_password2(self):
- cd = self.cleaned_data
- if cd['password'] != cd['password2']:
- raise forms.ValidationError("password don't match.")
- return cd['password2']
- class UserForm(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','password','password2']
- def clean_password2(self):
- cd = self.cleaned_data
- if cd['password'] != cd['password2']:
- raise forms.ValidationError("password don't match.")
- return cd['password2']
- class ProfileForm(forms.ModelForm):
- """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 = forms.ChoiceField(choices=CLG_CHOICES)
- branch = forms.ChoiceField(choices=BRANCH_CHOICES)
- birth_date = forms.DateField()"""
- class Meta:
- model = Profile
- fields = ['college','branch','birth_date']
- #views.py
- from django.shortcuts import render,redirect
- from . import forms
- from .forms import *
- from django.contrib.auth import login, authenticate
- from django.http import HttpResponse
- from django.views.generic import ListView,View
- # Create your views here.
- def index(request):
- return render(request,'register/index.html')
- class UserFormView(View):
- form_class = UserForm
- profile_class = ProfileForm
- template_name = "register/signup.html"
- def get(self, request):
- form = self.form_class(None)
- profile = self.profile_class(None)
- return render(request, self.template_name, {"form": form,'profile':profile})
- def post(self, request):
- form = self.form_class(request.POST)
- profile = self.profile_class(request.POST)
- if form.is_valid() and profile.is_valid():
- user = form.save(commit=False)
- username = form.cleaned_data["username"]
- password = form.cleaned_data["password"]
- if len(username) == 12:
- code = username[4]+username[5]
- branch = ["CS", "IT", "ME", "CE", "EC"]
- if code in branch:
- user.set_password(password)
- user.save()
- prof = profile.save(commit=False)
- prof.user = user
- prof.save() #giving error, NOT NULL constraint
- user = authenticate(username= username, password= password)
- if user is not None:
- if user.is_active:
- login(request, user)
- return redirect("register:index")
- return render(request, self.template_name, {"form": form,'profile':profile})
Add Comment
Please, Sign In to add comment