日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

django 快速实现注册

發(fā)布時間:2025/6/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 django 快速实现注册 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

  對于web開來說,用戶登陸、注冊、文件上傳等是最基礎(chǔ)的功能,針對不同的web框架,相關(guān)的文章非常多,但搜索之后發(fā)現(xiàn)大多都不具有完整性,對于想學(xué)習(xí)web開發(fā)的新手來說不具有很強的操作性;對于web應(yīng)用來說,包括數(shù)據(jù)庫的創(chuàng)建,前端頁面的開發(fā),以及中間邏輯層的處理三部分。

  本系列以可操作性為主,介紹如何通過django?web框架來實現(xiàn)一些簡單的功能。每一章都具有完整性和獨立性。使用新手在動手做的過程中體會web開發(fā)的過程,過程中細(xì)節(jié)請參考相關(guān)文檔。

本操作的環(huán)境:

===================

deepin?linux?2013(基于ubuntu)

python?2.7

Django?1.6.2

===================

?

創(chuàng)建項目與應(yīng)用 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

?

#創(chuàng)建項目 fnngj@fnngj-H24X:~/djpy$ django-admin.py startproject mysite3 fnngj@fnngj-H24X:~/djpy$ cd mysite3 #在項目下創(chuàng)建一個account應(yīng)用 fnngj@fnngj-H24X:~/djpy/mysite3$ python manage.py startapp account

目錄結(jié)構(gòu)如下:

打開mysite3/mysite3/settings.py文件,將應(yīng)用添加進去:

# Application definition INSTALLED_APPS = ('django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','account', )

?

?

設(shè)計Model(數(shù)據(jù)庫) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

?

打開mysite3/account/models.py文件,添加如下內(nèi)容

from django.db import models# Create your models here. class User(models.Model):username = models.CharField(max_length=50)password = models.CharField(max_length=50)email = models.EmailField()

按照慣例先創(chuàng)建數(shù)據(jù)庫,創(chuàng)建三個字段,用戶存放用戶名、密碼、email?地址等。

下面進行數(shù)據(jù)庫的同步

fnngj@fnngj-H24X:~/djpy/mysite3$ python manage.py syncdb Creating tables ... Creating table django_admin_log Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_groups Creating table auth_user_user_permissions Creating table auth_user Creating table django_content_type Creating table django_session Creating table account_user You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): yes 輸入yes/noUsername (leave blank to use 'fnngj'): 用戶名(默認(rèn)當(dāng)前系統(tǒng)用戶名) Email address: fnngj@126.com 郵箱地址 Password: 密碼 Password (again): 確認(rèn)密碼 Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)

最后生成的 account_user?表就是我們models.py?中所創(chuàng)建的User類。Django?提供了他們之間的對應(yīng)關(guān)系。

?

?

創(chuàng)建視圖(邏輯層) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

?

打開mysite3/account/views.py?文件:

#coding=utf-8 from django.shortcuts import render from django import forms from django.shortcuts import render_to_response from django.http import HttpResponse,HttpResponseRedirect from django.template import RequestContext from account.models import User#定義表單模型 class UserForm(forms.Form):username = forms.CharField(label='用戶名:',max_length=100)passworld = forms.CharField(label='密碼:',widget=forms.PasswordInput())email = forms.EmailField(label='電子郵件:')# Create your views here. def register(request):if request.method == "POST":uf = UserForm(request.POST)if uf.is_valid():#獲取表單信息username = uf.cleaned_data['username']passworld = uf.cleaned_data['passworld']email = uf.cleaned_data['email']#將表單寫入數(shù)據(jù)庫user = User()user.username = usernameuser.passworld = passworlduser.email = emailuser.save()#返回注冊成功頁面return render_to_response('success.html',{'username':username})else:uf = UserForm()return render_to_response('register.html',{'uf':uf})

的這個邏輯中主要做了幾件事,首先提供給用戶一個注冊頁面(register.html),UserForm類定義了表單在注冊頁面上的顯示。接受用戶填寫的表單信息,然后將表單信息寫入到數(shù)據(jù)庫,最后返回給用戶一個注冊成功的頁面(success.html)

?

?

創(chuàng)建模板文件(前端頁面) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

?

在邏輯層提到了兩個頁面,一個注冊頁,一個注冊成功頁面。所以我們要把這兩個頁面創(chuàng)建出來。

先在mysite3/account/目錄下創(chuàng)建templates目錄,接著在mysite3/account/templates/目錄下創(chuàng)建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}} <input type="submit" value = "ok" /> </form> </body> </html>

mysite3/account/templates/目錄下創(chuàng)建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><h1>恭喜{{username}},注冊成功!</h1> </form> </body> </html>

?

設(shè)置模板路徑

打開mysite3/mysite3/settings.py文件,在底部添加:

#template TEMPLATE_DIRS=('/home/fnngj/djpy/mysite3/account/templates' )

?

?

設(shè)置URL ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

?

設(shè)置URL的配置也是django?web框架的一大特色。打開mysite3/mysite3/urls.py:

from django.conf.urls import patterns, include, urlfrom django.contrib import admin admin.autodiscover()urlpatterns = patterns('',# Examples:# url(r'^$', 'mysite3.views.home', name='home'),# url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)),url(r'^account/', include('account.urls')), )

?

在mysite3/account/目錄下創(chuàng)建urls.py文件:

from django.conf.urls import patterns, url from account import viewsurlpatterns = patterns('',url(r'^$', views.register, name='register'),url(r'^register/$',views.register,name = 'register'), )

這里人配置表示:訪問

http://127.0.0.1:8000/account/

http://127.0.0.1:8000/account/register/

都會指向一個注冊頁面。

?

?

體驗注冊 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

?

當(dāng)所有工作都完成之后,下面體驗一下我們的注冊功能吧。

啟動服務(wù):

fnngj@fnngj-H24X:~/djpy/mysite3$ python manage.py runserver Validating models...0 errors found May 21, 2014 - 14:31:32 Django version 1.6.2, using settings 'mysite3.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

訪問注冊頁面:

因為在register.html?文件中加了一點樣式,所以看上去好看了一點,但作為一個審美的人觀念的人無法容忍這樣丑陋的頁面存在。

以免錯誤的再次出現(xiàn),

打開mysite3/mysite3/settings.py文件,將下面一行代碼注釋:

MIDDLEWARE_CLASSES = ('django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware',#'django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware', )

?

現(xiàn)在可以作為一個用戶去填寫表單了。

?

?

?

注冊的用戶哪兒去了??

?

你依然可以像昨天一樣去查詢數(shù)據(jù)庫,但為什么不利用admin呢?

打開mysite3/account/models.py文件:

from django.db import models from django.contrib import admin # Create your models here. class User(models.Model):username = models.CharField(max_length=50)password = models.CharField(max_length=50)email = models.EmailField()class UserAdmin(admin.ModelAdmin):list_display = ('username','email')admin.site.register(User,UserAdmin)

重新初始化數(shù)據(jù)庫:

fnngj@fnngj-H24X:~/djpy/mysite3$ python manage.py syncdb Creating tables ... Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)

登陸admin?后臺

http://127.0.0.1:8000/admin/

登陸所需要的用戶名和密碼就是我們第一次同步數(shù)據(jù)庫時所設(shè)置

然后我們就看到剛剛注冊的那個用戶信息。

?

本文以實現(xiàn)基本功能為主,當(dāng)然你可以在此基礎(chǔ)上做更多的事。對用戶注冊信息的驗證,需要再次確認(rèn)密碼,修改一個漂亮的樣式等。如果有用點個贊吧。

總結(jié)

以上是生活随笔為你收集整理的django 快速实现注册的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。