Development environment: django 2.0.2 tool is pycharm
First create a django project named blog first program named BlogUser
After the creation is complete, you need to create a new file named register.html under templates
- Repeat the previous step to create a new file called welcome.html for the welcome interface after successful login
- Write the following code in the register.html file to render a simple login registration page in the browser
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="/static/js/jquery-3.3.1.js"></script> <script> $(function (){ $("input[name='username']").blur(function () { //alert("") uname=$("input [name='username']").val() CSRF=$("input[name='csrfmiddlewaretoken']").val() $.ajax({ url:'/BlogUser/getUser', data:{'uname':uname,'csrfmiddlewaretoken':CSRF}, type:'POST', success:function (that) { if (dat=='True'){ $("span").html("Username already exists") } else{ $("span").html("Username can be used") } } }); }); }); </script> </head> <body> <h1>Welcome to registration</h1> <form action="/BlogUser/register" method="post"> {% csrf_token %} 用户名:<input type="text" name="username" value="{{ username }}"> <span>{{ error }}</span> <br> 密 码:<input type="password" name="pwd"><br> {# 性 别:<input type="radio" name="sex" value="女" checked>女#} {# <input type="radio" name="sex" value="男">男<br>#} {# Hobbies: <input type="checkbox" name="likes" value="basketball">basketball#} {# <input type="checkbox" name="likes" value="足球">足球#} {# <input type="checkbox" name="likes" value="乒乓球">乒乓球<br>#} {# address:#} {# <select>#} {# <option value="Beijing">Beijing</option>#} {# <option value="Shanghai">Shanghai</option>#} {# <option value="Guangzhou">Guangzhou</option>#} {# <option value="Shenzhen">Shenzhen</option>#} {# </select>#} {# <br>#} <input type="submit" value="注册"> </form> </body> </html>
- Write the following code in the welcome.html file to jump to the welcome page after successful registration on the registration page
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Successful registration, welcome {{bloguser.username }} to log in</h1>> <h1>The ID assigned to you by the database is: {{ bloguser.id }}</h1> </body> </html>
- Next, you need to write the following code in the urls under BlogUser
-
from django.contrib import admin from django.urls import path,include from .views import * urlpatterns = [ path('register', register), #register and register.html file name should be consistent ]
- In the views directory under BlogUser, write the following code to process the registration function request. Including forwarding and redirecting
-
from django.shortcuts import render,redirect,reverse from .models import * from django.http import HttpResponse # Create your views here. #Handle registration request function def register(request): if request.method=='GET': return render(request,'register.html') elif request.method=='POST': username = request.POST.get('username') pwd = request.POST.get('pwd') bloguser = BlogUser () bloguser.username=username bloguser.password = pwd try: bloguser.save() #Redirect user: virtual namespace show: function executed args: parameters return redirect(reverse('user:welcome',args=[bloguser.id])) except: return render(request,'register.html', {'username':username,'error':'username already exists'}) def welcome(request,id): bloguser=BlogUser.objects.get(pk=id) # forward the response return render(request,'welcome.html',{'bloguser':bloguser})
- Two simple fields username and password are set in the models directory under BlogUser
- Among them, models.py carries the object of the data and provides the curd (add, delete, modify and check) of the data to generate the database table
- Create a py_blog database in SQLyog (press ctrl+D directly in the database to create a new database)
- Then we need to change django's database connection engine (in the settings.py directory under the blog, find DATABAS)
-
Django connection database must have django driver as mysqlclient version 1.7.3+
-
It can be installed by pip install mysqlclient
-
Since the initial app was created at the beginning, django automatically generated it for the bloguser here. If not, you need to write it yourself