当前位置:网站首页>Django4.0 + web + MySQL 5.7 realize simple login operation

Django4.0 + web + MySQL 5.7 realize simple login operation

2022-07-25 08:47:00 Ding Jiaxiong

Django

Django + Web + MySQL5.7 Realize simple login operation

1.1 Django + Web + MySQL5.7

1.1.1 Create project

 Insert picture description here

1.1.2 Create login application (Django Often called App)
python manage.py startapp login

 Insert picture description here

1.1.3 register App

 Insert picture description here

1.1.4 Configuration database

Be careful :Django Cannot create database , You need to manually create it in advance

 Insert picture description here

DATABASES = {
    
    'default': {
    
    'ENGINE': 'django.db.backends.mysql',
    'NAME':'login',
    'USER': 'root',
    'PASSWORD': '200039',
    'HOST': '127.0.0.1',
    'PORT': 3306,
    }
}

 Insert picture description here

1.1.5 Create views and configurations urls

 Insert picture description here

 Insert picture description here

stay templates New under the directory login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1> The user login </h1>
    <form method="post" action="/login/">
        {% csrf_token %}
        <input type="text" name="user" placeholder=" user name ">
        <input type="password" name="pwd" placeholder=" password ">

        <button type="submit" value=" Submit "> Submit </button>
        {
   { error_msg }}
    </form>
</body>
</html>
1.1.6 Start the service access test

 Insert picture description here

Or execute the command on the command line

python manage.py runserver

 Insert picture description here

 Insert picture description here

1.1.7 Creating models ( Entity class )

 Insert picture description here

To execute a command on the command line

python manage.py makemigrations
python manage.py migrate

 Insert picture description here

view the database

 Insert picture description here

Manually insert a piece of data (ORM It's fine too , But not the focus of this example , ok , This example has no focus )

 Insert picture description here

1.1.8 Write processing logic views.py

 Insert picture description here

def login(request):
    if request.method == "GET":
        return render(request,"login.html")
    username = request.POST.get("user")
    password = request.POST.get("pwd")

    user = User.objects.get(id = 1)
    user_name = user.username
    pass_word = user.password

    if username == user_name and password == pass_word:
        return HttpResponse(" Login successful ")
    return render(request,"login.html",{
    "error_msg":" Login failed "})
1.1.9 Start the service , test

 Insert picture description here

Click on the submit

 Insert picture description here

Modify the information

 Insert picture description here

Submit

 Insert picture description here

原网站

版权声明
本文为[Ding Jiaxiong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/206/202207250841476174.html