简易django项目之登录验证
本站寻求有缘人接手,详细了解请联系站长QQ1493399855
该项目没有使用orm
views.py
from django.shortcuts import render
import pymysql# Create your views here.
def login(request):if request.method == 'GET':return render(request, 'login.html')elif request.method == 'POST':name = request.POST.get('name')pwd = request.POST.get('password')conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='123',database='djangotest')cursor = conn.cursor(pymysql.cursors.DictCursor)res = cursor.execute('SELECT * FROM USER WHERE USER=%s AND PASSWORD=%s', [name, pwd])if res:return render(request, 'login.html',{'msg':'登录成功!'})else:return render(request,'login.html',{'msg':'登录失败!'})
创建静态目录static
在settings中配置
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static'),
]
在static文件夹中创建css文件夹并创建login.css文件
h1{color: aquamarine;
}
.lg{background-color: rebeccapurple;width: 100%;height: 200px;
}
.log{width: 100px;height: 200px;margin: 0 auto;
}
在templates文件夹中创建login.html文件
<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><link rel="stylesheet" href="/static/css/login.css"><title>登录</title>
</head>
<body>
<div class="lg"><div class="log"><h1>登录</h1><form action="/login/" method="post"><input type="text" name="name" placeholder="请输入用户名:"><input type="password" name="password" placeholder="请输入密码:"><button type="submit">提交</button></form><div>{{ msg }}</div></div></div>
</body>
</html>
配置路由urls.py文件
from django.conf.urls import url
from django.contrib import admin
from app01.views import login
urlpatterns = [url(r'^admin/', admin.site.urls),url(r'^login/', login),url(r'^$',login)
]