Add new user registration
This commit is contained in:
parent
1ed03f7d53
commit
9e55b0a1ed
3 changed files with 60 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<body>
|
<body>
|
||||||
|
<h3>Log In</h3>
|
||||||
{% if login_failed %}
|
{% if login_failed %}
|
||||||
<p> Incorrect username or password! </p>
|
<p> Incorrect username or password! </p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -12,5 +13,23 @@
|
||||||
<input type="password" id="password" name="password"><br><br>
|
<input type="password" id="password" name="password"><br><br>
|
||||||
<input type="submit" value="Log in">
|
<input type="submit" value="Log in">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<h3>Register as New User</h3>
|
||||||
|
|
||||||
|
<form action="/accounts/register/" method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
<label for="username">Username:</label><br>
|
||||||
|
<input type="text" id="username" name="username"><br><br>
|
||||||
|
<label for="password">Password:</label><br>
|
||||||
|
<input type="password" id="password1" name="password1"><br><br>
|
||||||
|
<label for="password">Repeat Password:</label><br>
|
||||||
|
<input type="password" id="password2" name="password2"><br><br>
|
||||||
|
<ul>
|
||||||
|
{% for error in registration_errors %}
|
||||||
|
<li>{{error}}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
<input type="submit" value="Register">
|
||||||
|
</form>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -23,5 +23,6 @@ urlpatterns = [
|
||||||
path('remove/<int:note_id>/', views.remove, name='remove'),
|
path('remove/<int:note_id>/', views.remove, name='remove'),
|
||||||
path('search/', views.search, name='search'),
|
path('search/', views.search, name='search'),
|
||||||
path('accounts/login/', views.login_view, name='login'),
|
path('accounts/login/', views.login_view, name='login'),
|
||||||
|
path('accounts/register/', views.register_view, name='register'),
|
||||||
path('accounts/logout/', views.logout_view, name='logout'),
|
path('accounts/logout/', views.logout_view, name='logout'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
|
from django.contrib.auth.models import User
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.contrib.auth.validators import UnicodeUsernameValidator
|
||||||
from django.contrib.auth import authenticate, login, logout
|
from django.contrib.auth import authenticate, login, logout
|
||||||
|
from django.contrib.auth.password_validation import validate_password
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
from notes.models import Note
|
from notes.models import Note
|
||||||
|
|
||||||
|
@ -73,6 +77,42 @@ def login_view(request):
|
||||||
return redirect("index")
|
return redirect("index")
|
||||||
|
|
||||||
|
|
||||||
|
def register_view(request):
|
||||||
|
if request.method == 'POST':
|
||||||
|
errors = []
|
||||||
|
username = request.POST.get('username')
|
||||||
|
password1 = request.POST.get('password1')
|
||||||
|
password2 = request.POST.get('password2')
|
||||||
|
|
||||||
|
# Username validation
|
||||||
|
if User.objects.filter(username=username).first() != None:
|
||||||
|
errors.append("Username in use.")
|
||||||
|
try:
|
||||||
|
username_validator = UnicodeUsernameValidator()
|
||||||
|
username_validator(username)
|
||||||
|
except ValidationError as error:
|
||||||
|
for message in error.messages:
|
||||||
|
errors.append(message)
|
||||||
|
|
||||||
|
# Password validation
|
||||||
|
if password1 != password2:
|
||||||
|
errors.append("Passwords don't match.")
|
||||||
|
try:
|
||||||
|
validate_password(password1)
|
||||||
|
except ValidationError as error:
|
||||||
|
for message in error.messages:
|
||||||
|
errors.append(message)
|
||||||
|
|
||||||
|
if not errors:
|
||||||
|
user = User.objects.create_user(username=username, password=password1)
|
||||||
|
login(request, user)
|
||||||
|
return redirect("index")
|
||||||
|
else:
|
||||||
|
return render(request, 'login.html', { 'registration_errors' : errors })
|
||||||
|
|
||||||
|
return redirect("index")
|
||||||
|
|
||||||
|
|
||||||
def logout_view(request):
|
def logout_view(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
logout(request)
|
logout(request)
|
||||||
|
|
Reference in a new issue