[Django #12] Adding user authentication
This commit is contained in:
parent
c29ae7a47b
commit
5cd127f2c1
6 changed files with 96 additions and 0 deletions
24
kjg/IPv6/ula/ularegistry/templates/base.html
Normal file
24
kjg/IPv6/ula/ularegistry/templates/base.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<!-- change something -->
|
||||
{% if user.is_authenticated %}
|
||||
<h3> {{ user.username }} welcome </h3>
|
||||
<a href="{% url 'logout' %}">logout</a>
|
||||
{% else %}
|
||||
<a href="{% url 'signup' %}">signup</a>
|
||||
<a href="{% url 'login' %}">login</a>
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% block container %}
|
||||
{% endblock %}
|
||||
</body>
|
||||
</html>
|
11
kjg/IPv6/ula/ularegistry/templates/login.html
Normal file
11
kjg/IPv6/ula/ularegistry/templates/login.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
{% extends 'base.html' %}
|
||||
{% block container %}
|
||||
<h3>login</h3>
|
||||
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
|
||||
{{ login_form }}
|
||||
<input type="submit" value="Submit"/>
|
||||
</form>
|
||||
{% endblock %}
|
|
@ -1,3 +1,5 @@
|
|||
{% extends 'base.html' %}
|
||||
{% block container %}
|
||||
<form action="" method="post">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
|
@ -14,3 +16,4 @@
|
|||
{% else %}
|
||||
<p>No registered IP.</p>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
|
11
kjg/IPv6/ula/ularegistry/templates/signup.html
Normal file
11
kjg/IPv6/ula/ularegistry/templates/signup.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
{% extends 'base.html' %}
|
||||
{% block container %}
|
||||
<h3>sign-up</h3>
|
||||
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
{{ signup_form }}
|
||||
<input type="submit" name="Submit"/>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
|
@ -6,4 +6,7 @@ urlpatterns = [
|
|||
path('', views.index, name='index'),
|
||||
path('randomIP', views.randomIP, name='randomIP'),
|
||||
path('newrandomIP', views.newrandomIP, name='newrandomIP'),
|
||||
path('login/', views.login, name='login'),
|
||||
path('logout/', views.logout, name='logout'),
|
||||
path('signup/', views.signup, name='signup'),
|
||||
]
|
||||
|
|
|
@ -5,6 +5,10 @@ from ularegistry.models import ips, ips2
|
|||
from django.shortcuts import redirect
|
||||
from ularegistry.checkip import *
|
||||
from ularegistry.randomcreate import *
|
||||
from django.shortcuts import render, redirect
|
||||
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
|
||||
from django.contrib.auth import login as auth_login
|
||||
from django.contrib.auth import logout as auth_logout
|
||||
|
||||
# Create your views here.
|
||||
def index(request):
|
||||
|
@ -124,3 +128,43 @@ def newrandomIP(request):
|
|||
'ips_listr' : ips_listr,
|
||||
}
|
||||
return render(request, 'newrandomIP.html', ctx2)
|
||||
|
||||
|
||||
def signup(request):
|
||||
if request.user.is_authenticated:
|
||||
return redirect('newrandomIP')
|
||||
|
||||
if request.method == 'POST':
|
||||
signup_form = UserCreationForm(request.POST)
|
||||
if signup_form.is_valid():
|
||||
user = signup_form.save()
|
||||
auth_login(request, user)
|
||||
return redirect('login')
|
||||
else:
|
||||
print("fail singup")
|
||||
|
||||
else:
|
||||
signup_form = UserCreationForm()
|
||||
|
||||
return render(request, 'signup.html', {'signup_form':signup_form})
|
||||
|
||||
|
||||
def login(request):
|
||||
if request.user.is_authenticated:
|
||||
return redirect('newrandomIP')
|
||||
|
||||
if request.method == 'POST':
|
||||
login_form = AuthenticationForm(request, request.POST)
|
||||
if login_form.is_valid():
|
||||
auth_login(request, login_form.get_user())
|
||||
return redirect('newrandomIP')
|
||||
|
||||
else:
|
||||
login_form = AuthenticationForm()
|
||||
|
||||
return render(request, 'login.html', {'login_form' : login_form})
|
||||
|
||||
|
||||
def logout(request):
|
||||
auth_logout(request)
|
||||
return redirect('login')
|
||||
|
|
Loading…
Add table
Reference in a new issue