国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

django的登錄注冊系統(tǒng)的示例代碼

2020-02-23 00:07:06
字體:
供稿:網(wǎng)友

摘要

django框架本身自帶有登錄注冊,也可以自己寫登錄注冊,下面將介紹這這2種方式實登錄注冊

一、自己寫登錄注冊登出

1.注冊regist

注冊采用的是form表單,提交到數(shù)據(jù)庫,在登錄的時候,查詢數(shù)據(jù)看,看用戶有沒有注冊,如果用戶沒有注冊,則返回注冊頁面注冊

(1)models.py文件里創(chuàng)建相關(guān)的字段: 用戶名字/用戶密碼/cookies攜帶的ticket

from django.db import models# Create your models here.class Users(models.Model):  u_name = models.CharField(max_length=10)  u_password = models.CharField(max_length=255)  u_ticket = models.CharField(max_length=30, null=True)  class Meta:    db_table = 'day51_user'

(2)urls.py 配置相關(guān)路由

from django.conf.urls import urlfrom uauth import viewsurlpatterns = [  url(r'^regist/', views.regist),  url(r'^login/', views.login),  url(r'^logout', views.logout)]

(3)views.py 書寫regist方法

導入相關(guān)的包,在regist,login,logout都會使用到

import randomimport timefrom django.contrib import authfrom django.contrib.auth.hashers import make_password,check_passwordfrom django.contrib.auth.models import Userfrom django.http import HttpResponseRedirect, HttpResponsefrom django.shortcuts import renderfrom django.core.urlresolvers import reverse# Create your views here.from uauth.models import Users

如果用戶請求regist方法,則直接跳轉(zhuǎn)到相關(guān)的html頁面。

如果用戶在html頁面點擊了post按鈕,對密碼進行加密后,將數(shù)據(jù)提交到數(shù)據(jù)庫,并返回登錄login頁面。

獲得post提交的表單文字,使用request.POST.get(' ')

def regist(request):  if request.method == 'GET':    return render(request, 'day6_regist.html')  if request.method == 'POST':    # 注冊    name = request.POST.get('name')    password = request.POST.get('password')    # 對密碼進行加密    password = make_password(password)    Users.objects.create(u_name=name, u_password=password)    return HttpResponseRedirect('/uauth/login/')

編寫regist的提交表單, method方法選擇'POST'

文件目錄在templates下[圖片上傳中...(image.png-cc7763-1526105439415-0)]

{%csrf_token%}是針對提交的時候csrf跨域錯誤

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>注冊頁面</title></head><body><form action="" method="POST">  {% csrf_token %}  注冊姓名:<input type="text" name="name">  注冊密碼:<input type="password" name="password">  <input type="submit" value="提交"></form></body></html>

2.登錄login

(1)配置路由urls.py, 與注冊的時候一樣的操作

(2)配置views.py, 編寫login方法

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 会泽县| 永新县| 汶上县| 顺义区| 廉江市| 陈巴尔虎旗| 株洲县| 山丹县| 图们市| 正定县| 巨野县| 英山县| 吴旗县| 兴和县| 泰顺县| 舒城县| 瑞昌市| 淳安县| 甘孜| 胶南市| 大姚县| 房产| 齐河县| 贵定县| 怀柔区| 临汾市| 清新县| 建始县| 桐柏县| 宁远县| 化州市| 浦东新区| 舒城县| 甘孜| 招远市| 加查县| 齐河县| 南阳市| 安化县| 沙田区| 讷河市|