python: front end (HTML) + back end (Django) + database (MySQL)

Keywords: Python Django Database SQL

1. Create an html file for a simple web page registration demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>register</title>
</head>
<body>
<!--Form field action Used to submit acquired to background address-->
<form action="save" method="get"> <span> user:<input type="text" name="username"> </span> <br> <span> Password:<input type="password" name="password"> </span> <br> <span> <input type="submit" name="submit1" value="register"> </span> </form> </body> </html>

 

2. Create an html file for simple web page login demo

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sign in</title>
</head>
<body>
<form action="query" method="get">
    <span>
        user:<input type="text" name="username">
    </span>
    <br>
    <span>
        Password:<input type="password" name="password">
    </span>
    <br>
    <span>
        <input type="submit" name="submit1" value="Sign in">
    </span>

</form>
</body>
</html>

 

 

3. Create a Django project and put the two html files in the template template folder

 

4. Open the urls.py file generated by default under the Django project, and edit some business functions

"""Django01 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.shortcuts import render
from django.shortcuts import HttpResponse
import pymysql
#Login page
def login(request):
    #Specify the page to visit, render Function of: submit the requested page results to the client
    return render(request,'login.html')
#Registration page
def regiter(request):
    return render(request,'regiter.html')
#Define a function to save the registered data
def save(request):
    has_regiter = 0#Used to record whether the current account already exists, 0: does not exist 1: already exists
    a = request.GET#Obtain get()request
    #print(a)
    #adopt get()Request to get the data submitted in the previous paragraph
    userName = a.get('username')
    passWord = a.get('password')
    #print(userName,passWord)
    #Connect to database
    db = pymysql.connect('127.0.0.1','root','123','db2')
    #Create cursors
    cursor = db.cursor()
    #SQL Sentence
    sql1 = 'select * from user1'
    #implement SQL Sentence
    cursor.execute(sql1)
    #Query all data stored in all_users in
    all_users = cursor.fetchall()
    i = 0
    while i < len(all_users):
        if userName in all_users[i]:
            ##Indicates that the account already exists
            has_regiter = 1

        i += 1
    if has_regiter == 0:
        # Insert the user name and password into the database
        sql2 = 'insert into user1(username,password) values(%s,%s)'
        cursor.execute(sql2,(userName,passWord))
        db.commit()
        cursor.close()
        db.close()
        return HttpResponse('login was successful')
    else:

        cursor.close()
        db.close()
        return HttpResponse('The account already exists')

def query(request):
    a = request.GET
    userName = a.get('username')
    passWord = a.get('password')
    user_tup = (userName,passWord)
    db = pymysql.connect('127.0.0.1','root','123','db2')
    cursor = db.cursor()
    sql = 'select * from user1'
    cursor.execute(sql)
    all_users = cursor.fetchall()
    cursor.close()
    db.close()
    has_user = 0
    i = 0
    while i < len(all_users):
        if user_tup == all_users[i]:
            has_user = 1
        i += 1
    if has_user == 1:
        return HttpResponse('Login successfully')
    else:
        return HttpResponse('Incorrect user name or password')
urlpatterns = [
    path('admin/', admin.site.urls),#Created by default
    path('login/',login),#Used to open the login page
    path('regiter/',regiter),#Used to open the registration page
    path('regiter/save',save),#Enter the user name and password and give it to the background save Function processing
    path('login/query',query)#Enter the user name and password and give it to the background query Function processing

]

 

5. Run the entire Django project and access the corresponding url

Database data:

 

 

Background status information

To view the database data again:

6. Log in again and open the login url

 

 

Test a user name error:

Posted by curtis_b on Thu, 21 Nov 2019 07:45:04 -0800