First initial commit for test Django website
This commit is contained in:
0
users/__init__.py
Normal file
0
users/__init__.py
Normal file
3
users/admin.py
Normal file
3
users/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
users/apps.py
Normal file
6
users/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UsersConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'users'
|
||||
0
users/migrations/__init__.py
Normal file
0
users/migrations/__init__.py
Normal file
3
users/models.py
Normal file
3
users/models.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
17
users/templates/users/login.html
Normal file
17
users/templates/users/login.html
Normal file
@ -0,0 +1,17 @@
|
||||
{% extends 'layout.html' %}
|
||||
|
||||
{% block title %}
|
||||
User Login
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>User Login</h1>
|
||||
<form class="form-with-validation" action="/users/login/" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
{% if request.GET.next %}
|
||||
<input type="hidden" name="next" value="{{ request.GET.next }}" />
|
||||
{% endif %}
|
||||
<button class="form-submit">Submit</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
14
users/templates/users/user_register.html
Normal file
14
users/templates/users/user_register.html
Normal file
@ -0,0 +1,14 @@
|
||||
{% extends 'layout.html' %}
|
||||
|
||||
{% block title %}
|
||||
Register a New User
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Register a New User</h1>
|
||||
<form class="form-with-validation" action="/users/register/" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
<button class="form-submit">Submit</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
3
users/tests.py
Normal file
3
users/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
10
users/urls.py
Normal file
10
users/urls.py
Normal file
@ -0,0 +1,10 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
app_name = 'users'
|
||||
urlpatterns = [
|
||||
path('register/', views.register, name="register"),
|
||||
path('login/', views.login_view, name="login"),
|
||||
path('logout/', views.logout_view, name="logout"),
|
||||
#path('<slug:slug>', views.post_page, name="page"),
|
||||
]
|
||||
36
users/views.py
Normal file
36
users/views.py
Normal file
@ -0,0 +1,36 @@
|
||||
from django.shortcuts import render, redirect
|
||||
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
|
||||
from django.contrib.auth import login, logout
|
||||
|
||||
|
||||
|
||||
|
||||
# Create your views here.
|
||||
def register(request):
|
||||
if request.method=="POST":
|
||||
form = UserCreationForm(request.POST)
|
||||
if form.is_valid():
|
||||
login(request, form.save()) # from.save also returns the username
|
||||
return redirect("post:list")
|
||||
else:
|
||||
form = UserCreationForm()
|
||||
return render(request, 'users/user_register.html', {"form": form})
|
||||
|
||||
def login_view(request):
|
||||
if request.method=="POST":
|
||||
form = AuthenticationForm(data=request.POST)
|
||||
if form.is_valid():
|
||||
#login here
|
||||
login(request, form.get_user())
|
||||
if 'next' in request.POST:
|
||||
return redirect(request.POST.get('next'))
|
||||
else:
|
||||
return redirect("post:list")
|
||||
else:
|
||||
form = AuthenticationForm()
|
||||
return render(request, 'users/login.html', {"form": form})
|
||||
|
||||
def logout_view(request):
|
||||
if request.method == "POST":
|
||||
logout(request)
|
||||
return redirect("post:list")
|
||||
Reference in New Issue
Block a user