Django测试开发-13-优化表单提交(GET、POST、登录、注册)

一、GET请求

举例

article.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>article页面</title>
</head>
<body>

<form action="/vote/result/" method="get">

    <h1>文章录入!!!</h1>
    文章标题:<input type="text" name="title"/>
    文章标题:<input type="text" name="content"/>
    <br>
    <input type="submit" value="提交"/>

</form>

</body>
</html>

views.py

def article(request):

    return render(request,"article.html")

def result(request):

    print(request.method)
    print(request.GET['title'])
    print(request.GET['content'])
    return HttpResponse("提交成功")

urls.py

url(r"^article",views.article),
url(r"^result",views.result),

浏览器输入:http://127.0.0.1:8000/vote/article

 

 

 输入内容并提交    

GET
11111
11111

 二、POST表单提交

注册页面  register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
<h1>新用户注册!!!</h1>
<form action="/vote/register/" method="post">
    {% csrf_token %}
    <p>
        用户名:<input type="text" name="username" required="required">*{{ rename }}
    </p>

    <p>
        密 码:<input type="text" name="password" required="required">*
    </p>

    <p>
        手机号码:<input type="text" name="phone">
    </p>

    <p>
        <input type="submit" value="注册">
    </p>

</form>


</body>
</html>

登录页面 login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>

<h1>欢迎登录</h1>

<form action="/vote/login/" method="post">
    {% csrf_token %}
    <p>
        登录名:<input type="text" name="username" required="required">*
    </p>

    <p>
        密 码:<input type="text" name="password" required="required">*
    </p>

    <p>
        <input type="submit" value="登录">
    </p>

     {{ info }}
    {% if info  == "用户未注册!请注册" %}
        <a href="http://127.0.0.1:8000/vote/register">跳转至注册</a>
    {% endif %}

</form>

</body>
</html>

models.py

新增User表

class User(models.Model):

    username = models.CharField('登录名',max_length=20)
    password = models.CharField('登录密码',max_length=100)
    phone = models.CharField('手机号',max_length=20)

 

 

 

views.py

注册

def register(request):

    if request.method == "POST":
        # 获取用户输入
        username = request.POST.get("username",None)
        password = request.POST.get("password")
        phone = request.POST.get("phone")

        # 先从数据库查询是否已经注册过了
        user = User.objects.filter(username=username)
        print(user)

        if user:
            res = "%s用户名已经被注册,请更换一个" % username
            return render(request,'register.html',{'rename':res})
        else:
            se_user = User()
            se_user.username = username
            # 密文存储密码
            se_user.password = make_password(password)
            se_user.phone = phone
            # 保存到数据库
            se_user.save()

            return render(request,"login.html")

    return render(request,"register.html")

登录

def login(request):

    if request.method == "POST":

        username = request.POST.get("username",None)
        password = request.POST.get("password")
        user = User.objects.filter(username = username).first()

        if user is None:

            return render(request,"login.html",{'info':"用户未注册!请注册"})

        if user.username == username:
            result = check_password(password,user.password)
            print(result)

            if result:

                return HttpResponse("登录成功!")

            else:
                return render(request,"login.html",{'info':"用户名或者密码错误!"})


    else:
        return render(request,"login.html",{'info':""})

urls.py

url(r"^register",views.register),
url(r"^login",views.login),

 

 浏览器输入:http://127.0.0.1:8000/vote/register

 

posted @ 2020-03-16 18:23  旅行没有终点  阅读(280)  评论(0编辑  收藏  举报