最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Django小白教程之Django用戶注冊(cè)與登錄

 更新時(shí)間:2016年04月22日 11:06:48   作者:JohnaXu  
這篇文章主要介紹了Django小白教程之Django用戶注冊(cè)與登錄的相關(guān)資料,需要的朋友可以參考下

 Django 是由 Python 開發(fā)的一個(gè)免費(fèi)的開源網(wǎng)站框架,可以用于快速搭建高性能,優(yōu)雅的網(wǎng)站!

學(xué)習(xí)django學(xué)得超級(jí)吃力,最近弄個(gè)最簡(jiǎn)單的用戶登錄與注冊(cè)界面都是那么難,目前算是基本實(shí)現(xiàn)了,雖然功能特別特別簡(jiǎn)單但是做一個(gè)記錄,以后學(xué)習(xí)深入了再來(lái)補(bǔ)充:

首先創(chuàng)建項(xiàng)目,到項(xiàng)目所在目錄:django-admin startproject demo0414_userauth

進(jìn)入項(xiàng)目:cd demo0414_userauth

創(chuàng)建相應(yīng)的app:django-admin startapp account

整個(gè)項(xiàng)目的結(jié)構(gòu)圖如圖所示

├── account
│ ├── admin.py
│ ├── admin.pyc
│ ├── apps.py
│ ├── init.py
│ ├── init.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── init.py
│ │ └── init.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── demo0414_userauth
│ ├── init.py
│ ├── init.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── manage.py
└── templates
├── register.html
├── success.html
└── userlogin.html

4 directories, 29 files

然后在setting文件的installed_app中添加app account;

添加app 

創(chuàng)建一個(gè)templates文件夾,可以放在項(xiàng)目的根目錄下也可以放在app的目錄下。一般情況下提倡放在app的目錄下。如果放下項(xiàng)目的根目錄下需要在setting文件中TEMPLATES中設(shè)置'DIRS': [os.path.join(BASE_DIR,'templates')],否則不能使用模板。

這里寫圖片描述 

另外因?yàn)檫@個(gè)項(xiàng)目存在頁(yè)面跳轉(zhuǎn)的問(wèn)題,為了安全防止csrf攻擊,一把模板中都有了相關(guān)的設(shè)置。目前我還不會(huì)用這個(gè)東西,據(jù)說(shuō)在form表單中添加標(biāo)簽{% csrf_token %}就可以實(shí)現(xiàn)了,但是我沒(méi)有成功。所以先不考慮這個(gè)問(wèn)題,把seeting中的這個(gè)中間件'django.middleware.csrf.CsrfViewMiddleware',注釋掉

這里寫圖片描述 

然后在model中創(chuàng)建相應(yīng)的數(shù)據(jù)庫(kù):

class User(models.Model):
 username = models.CharField(max_length=50)
 password = models.CharField(max_length=50)
 email = models.EmailField()

view中添加相應(yīng)的程序。Pdb當(dāng)時(shí)用于斷點(diǎn)調(diào)試,我很喜歡,超級(jí)喜歡。如果你不敢興趣,直接注釋即可。

#coding=utf-8
from django.shortcuts import render,render_to_response
from django import forms
from django.http import HttpResponse,HttpResponseRedirect
from django.template import RequestContext
from django.contrib import auth
from models import User

import pdb

def login(request): 
 if request.method == "POST":
  uf = UserFormLogin(request.POST)
  if uf.is_valid():
   #獲取表單信息
   username = uf.cleaned_data['username']
   password = uf.cleaned_data['password']   
   userResult = User.objects.filter(username=username,password=password)
   #pdb.set_trace()
   if (len(userResult)>0):
    return render_to_response('success.html',{'operation':"登錄"})
   else:
    return HttpResponse("該用戶不存在")
 else:
  uf = UserFormLogin()
return render_to_response("userlogin.html",{'uf':uf})
def register(request):
 curtime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime());
 if request.method == "POST":
  uf = UserForm(request.POST)
  if uf.is_valid():
   #獲取表單信息
   username = uf.cleaned_data['username']
   #pdb.set_trace()
   #try:
   filterResult = User.objects.filter(username = username)
   if len(filterResult)>0:
    return render_to_response('register.html',{"errors":"用戶名已存在"})
   else:
    password1 = uf.cleaned_data['password1']
    password2 = uf.cleaned_data['password2']
    errors = []
    if (password2 != password1):
     errors.append("兩次輸入的密碼不一致!")
     return render_to_response('register.html',{'errors':errors})
     #return HttpResponse('兩次輸入的密碼不一致!,請(qǐng)重新輸入密碼')
    password = password2
    email = uf.cleaned_data['email']
   #將表單寫入數(shù)據(jù)庫(kù)
    user = User.objects.create(username=username,password=password1)
    #user = User(username=username,password=password,email=email)
    user.save()
    pdb.set_trace()
   #返回注冊(cè)成功頁(yè)面
    return render_to_response('success.html',{'username':username,'operation':"注冊(cè)"})
 else:
  uf = UserForm()
return render_to_response('register.html',{'uf':uf})
class UserForm(forms.Form):
 username = forms.CharField(label='用戶名',max_length=100)
 password1 = forms.CharField(label='密碼',widget=forms.PasswordInput())
 password2 = forms.CharField(label='確認(rèn)密碼',widget=forms.PasswordInput())
 email = forms.EmailField(label='電子郵件')
class UserFormLogin(forms.Form):
 username = forms.CharField(label='用戶名',max_length=100)
 password = forms.CharField(label='密碼',widget=forms.PasswordInput())

Tempaltes文件夾下總共有3個(gè)頁(yè)面:

Register.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>用戶注冊(cè)</title>
</head>
 <style type="text/css">
 body{color:#efd;background:#453;padding:0 5em;margin:0}
 h1{padding:2em 1em;background:#675}
 h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em}
 p{margin:1em 0}
 </style>
<body>
<h1>注冊(cè)頁(yè)面:</h1>
<form method = 'post' enctype="multipart/form-data">
{{uf.as_p}}
{{errors}}
</br>
<input type="submit" value = "ok" />
</form>
</body>
</html>

Userlogin.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>用戶注冊(cè)</title>
</head>
 <style type="text/css">
 body{color:#efd;background:#453;padding:0 5em;margin:0}
 h1{padding:2em 1em;background:#675}
 h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em}
 p{margin:1em 0}
 </style>
<body>
<h1>登錄頁(yè)面:</h1>
<form method = 'post' enctype="multipart/form-data">
{{uf.as_p}}
<input type="submit" value = "ok" />
</form>
</body>
</html>

Success.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title></title>
</head>
<body>
<form method = 'post'>
 <h1>恭喜,{{operation}}成功!</h1>
</form>
</body>
</html>

更新數(shù)據(jù)庫(kù):

這里寫圖片描述 

運(yùn)行服務(wù)器:

這里寫圖片描述 

注冊(cè)頁(yè)面:

這里寫圖片描述 

如果注冊(cè)的用戶沒(méi)有注冊(cè)過(guò),則能注冊(cè)成功點(diǎn)擊OK進(jìn)入success界面

登錄頁(yè)面:

這里寫圖片描述 

點(diǎn)擊OK就能進(jìn)入到success頁(yè)面

關(guān)于Django用戶注冊(cè)與登錄教程就給大家介紹完了,希望對(duì)大家有所幫助!

相關(guān)文章

  • Python手機(jī)號(hào)碼歸屬地查詢代碼

    Python手機(jī)號(hào)碼歸屬地查詢代碼

    這篇文章主要介紹了Python手機(jī)號(hào)碼歸屬地查詢代碼的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • Python算法練習(xí)之二分查找算法的實(shí)現(xiàn)

    Python算法練習(xí)之二分查找算法的實(shí)現(xiàn)

    二分查找也稱折半查找(Binary Search),它是一種效率較高的查找方法。本文將介紹python如何實(shí)現(xiàn)二分查找算法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2022-06-06
  • Python3.X 線程中信號(hào)量的使用方法示例

    Python3.X 線程中信號(hào)量的使用方法示例

    信號(hào)量semaphore 是一個(gè)變量,控制著對(duì)公共資源或者臨界區(qū)的訪問(wèn)。信號(hào)量維護(hù)著一個(gè)計(jì)數(shù)器,指定可同時(shí)訪問(wèn)資源或者進(jìn)入臨界區(qū)的線程數(shù)。下面這篇文章主要給大家介紹了關(guān)于Python3.X 線程中信號(hào)量的使用方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-07-07
  • Python 中使用正則表達(dá)式轉(zhuǎn)義

    Python 中使用正則表達(dá)式轉(zhuǎn)義

    這篇文章主要介紹了Python 正則表達(dá)式轉(zhuǎn)義,在 Python 中,正則表達(dá)式轉(zhuǎn)義sub()方法用于替換字符串,替換后的字符串由 re 模塊中的 Python 內(nèi)置方法返回,需要的朋友可以參考下
    2023-06-06
  • Python實(shí)現(xiàn)的在特定目錄下導(dǎo)入模塊功能分析

    Python實(shí)現(xiàn)的在特定目錄下導(dǎo)入模塊功能分析

    這篇文章主要介紹了Python實(shí)現(xiàn)的在特定目錄下導(dǎo)入模塊功能,結(jié)合實(shí)例形式分析了Python基于系統(tǒng)函數(shù)及import語(yǔ)句實(shí)現(xiàn)模塊導(dǎo)入的相關(guān)操作技巧,需要的朋友可以參考下
    2019-02-02
  • 用python 實(shí)現(xiàn)在不確定行數(shù)情況下多行輸入方法

    用python 實(shí)現(xiàn)在不確定行數(shù)情況下多行輸入方法

    今天小編就為大家分享一篇用python 實(shí)現(xiàn)在不確定行數(shù)情況下多行輸入方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Pycharm無(wú)法顯示動(dòng)態(tài)圖片的解決方法

    Pycharm無(wú)法顯示動(dòng)態(tài)圖片的解決方法

    今天小編就為大家分享一篇Pycharm無(wú)法顯示動(dòng)態(tài)圖片的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Python實(shí)現(xiàn)從PPT中導(dǎo)出高分辨率圖片

    Python實(shí)現(xiàn)從PPT中導(dǎo)出高分辨率圖片

    這篇文章主要為大家分享了一個(gè)實(shí)用腳本——如何利用Python實(shí)現(xiàn)從PPT中導(dǎo)出高分辨率(高 dpi)的圖片,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2023-05-05
  • Python中with及contextlib的用法詳解

    Python中with及contextlib的用法詳解

    這篇文章主要介紹了Python中with及contextlib的用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了with及contextlib的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-06-06
  • python self,cls,decorator的理解

    python self,cls,decorator的理解

    在python里面,self, cls 不是關(guān)鍵字,完全可以使用自己寫的任意變量代替實(shí)現(xiàn)一樣的效果
    2009-07-07

最新評(píng)論

鄂尔多斯市| 长沙市| 南陵县| 桂林市| 白朗县| 德兴市| 靖西县| 吉木萨尔县| 泸州市| 普安县| 宁德市| 子洲县| 胶南市| 仪陇县| 香格里拉县| 峡江县| 望谟县| 南川市| 乌苏市| 岱山县| 沈阳市| 荣成市| 渝中区| 屯门区| 石渠县| 西吉县| 双江| 昌黎县| 洱源县| 镇赉县| 京山县| 大余县| 闽清县| 鄂温| 遵化市| 泾川县| 玉山县| 穆棱市| 措美县| 揭西县| 当涂县|