django登录连接html,Django——登录功能(连接mysql)
一、路由url(r'^login',?views2.login),
url(r'^index',?views2.index),
url(r'^test',?views.test),
二、視圖函數(shù)
導入pymysql模塊def?index(request):????return?render(request,?'index2.html')def?login(request):????if?request.method?==?'GET':????????return?render(request,?'login.html')????else:
name?=?request.POST.get('name')
password?=?request.POST.get('password')????????#?創(chuàng)建一個數(shù)據(jù)庫鏈接
conn?=?pymysql.connect(host='127.0.0.1',?user='root',?password='123',?database='userinfo',?port=3306,?)????????#?拿到一個游標
cursor?=?conn.cursor()????????#?執(zhí)行sql
cursor.execute('select?*?from?user?where?name=%s?and?password=%s?',?(name,?password))????????#?獲取結(jié)果
ret?=?cursor.fetchone()????????print(ret)????????if?ret:????????????return?redirect('/index')????????else:????????????return?HttpResponse('用戶名或密碼錯誤')
三、模板文件login.htmlTitle
登錄功能
用戶名:密碼:四、模板文件index2.html總結(jié)
以上是生活随笔為你收集整理的django登录连接html,Django——登录功能(连接mysql)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软调整 Win11 策略:引入 URI
- 下一篇: 服务器修改数据库值,服务器上怎么修改数据