Create a login app
python manage.py startapp login
Add app
settings.py
INSTALLED_APPS = [ 'login', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
Write html file
templates/login/login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>高考推荐系统登录</title> </head> <body> <form action="" method="post"> <p>username: <input type="text" name="username" /></p> <p>password: <input type="text" name="password" /></p> <input type="submit" value="登录" /> </form> </body> </html>
templates/login/register.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册界面</title> </head> <body> <h1>注册界面</h1> <form action="" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> 确认密码:<input type="password" name="re_password"><br> <input type="submit" value="注册"> </form> </body> </html>
Create a database
login/models.py
from django.db import models class User(models.Model): username = models.CharField(max_length=20, unique=True) password = models.CharField(max_length=20) def __str__(self): return self.username
Database Migration
python manage.py makemigrations login python manage.py migrate
Create a view
Import the render django.shortcuts from, the HttpResponse, the redirect from the User login.models Import from django.views.decorators.csrf Import csrf_exempt @csrf_exempt DEF Login (Request): # Request This request is sent to the front end of the request, carries all data, django gave us deal with some of the columns, packaged as an object pass over # is actually very simple, learn to use it to give you some ways like, in fact, you also think of how it is handled. request.method == IF 'the GET': return the render (Request, 'Login / the login.html') IF request.method == 'the POST': username = request.POST.get ( 'username') password of request.POST = .get ( 'password') USER_OBJ = User.objects.filter (username = username, password = password) .first () IF USER_OBJ: return HttpResponse('登陆成功') return HttpResponse('用户名或密码错误') @csrf_exempt def register(request): if request.method == 'GET': return render(request, 'login/register.html') if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') re_password = request.POST.get('re_password') if username and password and re_password: if password == re_password: user_obj = User.objects.filter(username=username).first() if user_obj: return HttpResponse ( 'user already exists') the else: User.objects.create (username = username, password = password) .save () return redirect('/login/') else: return HttpResponse ( 'two passwords do not match') the else: return HttpResponse ( 'not free!')
Creating urls
login/urls.py
from django.urls import path from . import views urlpatterns = [ path('', views.login, name='login'), path('register', views.register, name='register'), ]
Creating overall routing file
urls.py
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('login/', include('login.urls')), ]
Administrator Management (optional)
login/admin.py
from django.contrib import admin from .models import User # Register your models here. admin.site.register(User)
Adding superuser
python manage.py createsuperuser
Login to enter administrator interface, easy to operate database
127.0.0.1:8000/admin