欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django小白教程之Django用戶注冊與登錄

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

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

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

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

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

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

整個項目的結(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)建一個templates文件夾,可以放在項目的根目錄下也可以放在app的目錄下。一般情況下提倡放在app的目錄下。如果放下項目的根目錄下需要在setting文件中TEMPLATES中設(shè)置'DIRS': [os.path.join(BASE_DIR,'templates')],否則不能使用模板。

這里寫圖片描述 

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

這里寫圖片描述 

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

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)時用于斷點調(diào)試,我很喜歡,超級喜歡。如果你不敢興趣,直接注釋即可。

#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('兩次輸入的密碼不一致!,請重新輸入密碼')
    password = password2
    email = uf.cleaned_data['email']
   #將表單寫入數(shù)據(jù)庫
    user = User.objects.create(username=username,password=password1)
    #user = User(username=username,password=password,email=email)
    user.save()
    pdb.set_trace()
   #返回注冊成功頁面
    return render_to_response('success.html',{'username':username,'operation':"注冊"})
 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個頁面:

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>用戶注冊</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>注冊頁面:</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>用戶注冊</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>登錄頁面:</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ù)庫:

這里寫圖片描述 

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

這里寫圖片描述 

注冊頁面:

這里寫圖片描述 

如果注冊的用戶沒有注冊過,則能注冊成功點擊OK進(jìn)入success界面

登錄頁面:

這里寫圖片描述 

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

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

相關(guān)文章

  • Windows下Anaconda2安裝NLTK教程

    Windows下Anaconda2安裝NLTK教程

    這篇文章主要為大家詳細(xì)介紹了Windows下Anaconda2安裝NLTK的教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • python3 tcp的粘包現(xiàn)象和解決辦法解析

    python3 tcp的粘包現(xiàn)象和解決辦法解析

    這篇文章主要介紹了python3 tcp的粘包現(xiàn)象和解決辦法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Pytorch加載圖像數(shù)據(jù)集的方法

    Pytorch加載圖像數(shù)據(jù)集的方法

    這篇文章主要介紹了Pytorch加載圖像數(shù)據(jù)集的方法,加載圖像數(shù)據(jù)集(這里以分類為例),通常都需要經(jīng)過兩個步驟:定義數(shù)據(jù)集和創(chuàng)建Dataloader數(shù)據(jù)加載器,本文通過代碼示例和圖文講解的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • python數(shù)據(jù)分析繪圖可視化

    python數(shù)據(jù)分析繪圖可視化

    這篇文章主要介紹了python數(shù)據(jù)分析繪圖可視化,數(shù)據(jù)可視化旨在直觀展示信息的分析結(jié)果和構(gòu)思,令某些抽象數(shù)據(jù)具象化,這些抽象數(shù)據(jù)包括數(shù)據(jù)測量單位的性質(zhì)或數(shù)量
    2022-06-06
  • 為什么說python適合寫爬蟲

    為什么說python適合寫爬蟲

    在本文中,小編給讀者們整理的一篇關(guān)于分析為什么說python適合寫爬蟲的語言的相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • 使用Python和wxPython實現(xiàn)下載視頻封面

    使用Python和wxPython實現(xiàn)下載視頻封面

    這篇文章主要為大家詳細(xì)介紹了如何使用Python和wxPython實現(xiàn)下載視頻封面,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • python 線程的五個狀態(tài)

    python 線程的五個狀態(tài)

    這篇文章主要介紹了python 線程的五個狀態(tài),幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09
  • PyTorch梯度裁剪避免訓(xùn)練loss nan的操作

    PyTorch梯度裁剪避免訓(xùn)練loss nan的操作

    這篇文章主要介紹了PyTorch梯度裁剪避免訓(xùn)練loss nan的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python腳本實現(xiàn)格式化css文件

    Python腳本實現(xiàn)格式化css文件

    這篇文章主要介紹了Python腳本實現(xiàn)格式化css文件,本文直接給出實現(xiàn)代碼,實現(xiàn)把壓縮后的CSS文件轉(zhuǎn)換成正??勺x的CSS格式,需要的朋友可以參考下
    2015-04-04
  • python創(chuàng)建與遍歷二叉樹的方法實例

    python創(chuàng)建與遍歷二叉樹的方法實例

    這篇文章主要給大家介紹了關(guān)于python創(chuàng)建與遍歷二叉樹的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論