forked from devmahmud/Django-Poll-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
68 lines (58 loc) · 2.76 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from .forms import UserRegistrationForm
from django.contrib import messages
from django.http import HttpResponse
def login_user(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
redirect_url = request.GET.get('next', 'home')
return redirect(redirect_url)
else:
messages.error(request, "Username Or Password is incorrect!!",
extra_tags='alert alert-warning alert-dismissible fade show')
return render(request, 'accounts/login.html')
def logout_user(request):
logout(request)
return redirect('home')
def create_user(request):
if request.method == 'POST':
check1 = False
check2 = False
check3 = False
form = UserRegistrationForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password1 = form.cleaned_data['password1']
password2 = form.cleaned_data['password2']
email = form.cleaned_data['email']
if password1 != password2:
check1 = True
messages.error(request, 'Password doesn\'t matched',
extra_tags='alert alert-warning alert-dismissible fade show')
if User.objects.filter(username=username).exists():
check2 = True
messages.error(request, 'Username already exists',
extra_tags='alert alert-warning alert-dismissible fade show')
if User.objects.filter(email=email).exists():
check3 = True
messages.error(request, 'Email already registered',
extra_tags='alert alert-warning alert-dismissible fade show')
if check1 or check2 or check3:
messages.error(
request, "Registration Failed", extra_tags='alert alert-warning alert-dismissible fade show')
return redirect('accounts:register')
else:
user = User.objects.create_user(
username=username, password=password1, email=email)
messages.success(
request, f'Thanks for registering {user.username}!', extra_tags='alert alert-success alert-dismissible fade show')
return redirect('accounts:login')
else:
form = UserRegistrationForm()
return render(request, 'accounts/register.html', {'form': form})