一个小小的作业

作业:

Django + pymysql实现

   -用户登录

   -查看用户列表

代码如下:非常简单的实现

这是urls.py

#urls.py
"""s4day64mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import  HttpResponse,render,redirect
import pymysql
def checkName(u,p):
    db = pymysql.connect('localhost', 'root', '8shiPment')
    cursor = db.cursor()

    sql = "use mydatabase"
    cursor.execute(sql)
    sql = "select * from member"
    flag = False
    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            if u == row[0] and p == row[1]:
                flag = True
    except Exception as e:
        raise
    return flag == True

def getUsers():
    db = pymysql.connect('localhost', 'root', '8shiPment')
    cursor = db.cursor()

    sql = "use mydatabase"
    cursor.execute(sql)
    sql = "select * from member"
    users = []
    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            users.append(row[0])
    except Exception as e:
        raise
    return users
    pass

def login(request):
    # print('nihaoya')
    # return HttpResponse('<h1>login</h1>')
    if request.method == "GET":
        return render(request,'login.html',{'msg':'你好呀'})
    else:
        u = request.POST.get('username')
        p = request.POST.get('password')
        print(u,p)
        if checkName(u,p)==True:
            # return redirect('https://www.baidu.com/')
            users = getUsers()
            return render(request, 'index.html', {'users': users})
            pass
        else:
            return render(request, 'login.html',{'msg':'用户名或者密码错误'})
            pass
    pass

def index(request):
    # return HttpResponse('你好呀')
    return render(
        request,
        'index.html',
        {'name':'蔡志威','users':['力宏','李杰']}
    )
urlpatterns = [
    # url(r'^admin/', admin.site.urls),
    url(r'^login/', login),
    url(r'^index/', index),
]

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" type="text/css" href="/static/commons.css">
<center>
<body>
    <h1>用户登录</h1>
    <form method="post" action="/login/">
        <br>用户名<input type="text" name="username"/>
        <br>密码<input type="password" name="password"/>
        <br><input type="submit" name="submit" value="登陆"/>
        <h1>循环</h1>
    <u1>
        {% for item in users %}
            {{item}}
        {% endfor %}
    </u1>
    </form>
</body>
</center>
</html>

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" type="text/css" href="/static/commons.css">
<center>
<body>
    <h1>用户登录</h1>
    <form method="post" action="/login/">
        <br>用户名<input type="text" name="username"/>
        <br>密码<input type="password" name="password"/>
        <br><input type="submit" name="submit" value="登陆"/>
        <br>{{msg }}
    </form>
</body>
</center>
</html>

comons.css

body
{
	background-color:#d0e4fe;
}
h1
{
	color:orange;
	text-align:center;
}

目录结构如下:

全部代码在这里https://gitee.com/caizhw3/python9_oldboy_code_learn/tree/master

猜你喜欢

转载自blog.csdn.net/czw0723/article/details/86586622