SHARE
TWEET

Untitled

a guest Aug 13th, 2019 141 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.shortcuts import render, redirect
  2. from django.contrib.auth import authenticate, login
  3. from customuser.models import user_type, User
  4.  
  5. def signup(request):
  6.     if (request.method == 'POST'):
  7.         email = request.POST.get('email')
  8.         password = request.POST.get('password')
  9.         st = request.POST.get('student')
  10.         te = request.POST.get('teacher')
  11.        
  12.         user = User.objects.create_user(
  13.             email=email,
  14.         )
  15.         user.set_password(password)
  16.         user.save()
  17.        
  18.         usert = None
  19.         if st:
  20.             usert = user_type(user=user,is_student=True)
  21.         elif te:
  22.             usert = user_type(user=user,is_teach=True)
  23.        
  24.         usert.save()
  25.         #Successfully registered. Redirect to homepage
  26.         return redirect('home')
  27.     return render(request, 'register.html')
  28.    
  29. def login(request):
  30.     if (request.method == 'POST'):
  31.         email = request.POST.get('email') #Get email value from form
  32.         password = request.POST.get('password') #Get password value from form
  33.         user = authenticate(request, email=email, password=password)
  34.        
  35.         if user is not None:
  36.             login(request, user)
  37.             type_obj = user_type.objects.get(user=user)
  38.             if user.is_authenticated and type_obj.is_student:
  39.                 return redirect('shome') #Go to student home
  40.             elif user.is_authenticated and type_obj.is_teach:
  41.                 return redirect('thome') #Go to teacher home
  42.         else:
  43.             # Invalid email or password. Handle as you wish
  44.             return redirect('home')
  45.  
  46.     return render(request, 'home.html')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top