Django landed in a small case

Profile : Log in Small Case

Message : The purpose of this article is to simply use the Django framework, provide some thoughts logged on, is not directly take to run, mainly veiws file login function, is the key: the logical log, the database how to retrieve data. At the same time did not join the network, it is insufficient, only for their own play, the current capacity is limited hope Haihan.

Page : Two simple html page, respectively login page and main page. The main page is a login form, main page saying, displays a user logs in successfully

Function : obtained from the user database username, password, compared with user input:
if there are (1) determines the user data;
(2) determines whether the password is correct;
(3) the account password is correct, the page jumping

General appearance:
Here Insert Picture Description

Detailed design (major code):

login page:

<body>


<form action="/app01/login/" method="post">
    {% csrf_token %}	{# 注意没有回报错 #}
    <p>
       用户: <input type="text" name="username">
    </p>
    <p>
       密码: <input type="text" name="password">
    </p>
    <p>
        <input type="submit" value="登录">
    </p>

</form>

<span>{{   err_str }}</span>

</body>

main page:

<body>

恭喜{{ username }} <br>{#显示登录人的名字#}
登录成功

</body>

File views:

from django.shortcuts import render,HttpResponse,redirect
from app01 import models
# Create your views here.

def login(request):
    # 错误提示语句
    err_str=''
    # 判断,如果是post请求,并且账户密码都对,返回nice
    if request.method=='POST':
        # 获取到用户输入的用户名密码
        user=request.POST.get('username')
        pwd=request.POST.get('password')
        # print(user,pwd)
        #获取数据库中所有的对象
        obj_list=models.User_login.objects.all()
        # 创建一个字典
        dic={}
        # 将对象的name 和password 封装成一个键值对,存到字典
        for i in obj_list:
            dic[i.name]=i.password
        # 判断用户是否存在,不存在返回 “用户不存在”
        if user not in dic:
            err_str='用户不存在'
            return render(request,'app01/login.html',{'err_str':err_str})
        # 如果存在,判断密码是否相等,不等返回 “密码错误”
        elif pwd != dic[user]:
            err_str = '密码错误'
            return render(request, 'app01/login.html', {'err_str': err_str})
        # 密码账户都通过,返回 “OK”
        else:
            return render(request,'app01/main.html',{'username':user})#####################
            # return redirect('/app01/main/')

    print(request.method)
    return  render(request,'app01/login.html')

models file:

from django.db import models

# Create your models here.
'''
主要目的生成数据表,表中包含id,name,password三个字段
'''
class User_login(models.Model):
    id=models.AutoField(primary_key=True)#id字段,主键自动增长
    name=models.CharField(null=False,max_length=20)#name字段,不能为空,字长20
    password=models.CharField(null=False,max_length=11)#password字段,不能为空,长度11

Execute the command :
Here Insert Picture Description
add data:
Here Insert Picture Descriptionwe look from a command prompt:
Here Insert Picture Description

app urls file under:

Here Insert Picture Description

urls file in the root:

Here Insert Picture Description

The main thing is to write this, there are some simple configuration templates, and some very simple configuration, not write.

Results Figure:

Address bar:
Here Insert Picture Descriptionthe Login page:
Here Insert Picture Description
Logon failure:
(1) user does not exist
Here Insert Picture Description
(2) Password Error
Here Insert Picture Description
main pages:
Success:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_39062888/article/details/89525866