五【用django2.0來開發(fā)】實現(xiàn)會員注冊功能-創(chuàng)新互聯(lián)

上一節(jié)我們完成了會員功能的后臺管理, 這一節(jié)我們需要完成會員注冊功能, 涉及到以下幾個模塊

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的玉溪網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
  1. URL配置
  2. views視圖模塊
  3. 模板
  4. Request/Response對象的使用

項目地址:https://gitee.com/ccnv07/django_example

URL路由配置

django是通過項目的urls.py文件來定義網(wǎng)站的url路由, 在我們的項目中是cms/urls.py文件

django的基本訪問流程

  1. 訪問url時, 通過cms/urls.py中定義的url路由, 獲取到要執(zhí)行的視圖函數(shù)或者類(保存在views.py)
  2. 將Request(請求對象)轉(zhuǎn)發(fā)到指定的視圖函數(shù)/類中, 執(zhí)行視圖函數(shù)/類的代碼
  3. 通過模板渲染(沒有模板則是其他的JsonResponse等資源對象), 將結(jié)果數(shù)據(jù)返回并輸出到瀏覽器中

打開cms/urls.py文件

from django.contrib import admin
from django.urls import path
urlpatterns = [
    path('admin/', admin.site.urls),
]

urlpatterns是整個路由的一個list, 是django定義的固定名稱

path函數(shù)
path(route, view, kwargs=None, name=None)
route: 指定訪問的路由
view: 是指定訪問的視圖函數(shù)/類
name: 是指定這條路由的名稱, 通過這個名稱, 我們就可以生成一個可訪問的url

默認的這一條路由, 就是定義了整個后臺的訪問url, 都是以admin/開頭的url, django會將admin.site.urls中定義的路由都加載過來

比如我們之前做的后臺管理功能, url就是:/admin/account/account/

路由route參數(shù)的格式

1. 固定字符串路由

這種路由是固定訪問的url, 不會發(fā)生變化, 比如關(guān)于我的訪問頁面.

urlpatterns = [
    path('about/', views.about),
]
2. 帶有變量的url路由, 比如我們訪問指定欄目下的文章
urlpatterns = [
    path('list/<int:nav_id>', views.list),
]

這種應(yīng)該用會比較多一些, <int:>指定這個nav_id必須是數(shù)字類型, 會執(zhí)行類型強制轉(zhuǎn)換, 而nav_id就是參數(shù)名, 通過以下的方式, 就可以訪問到這個參數(shù)。

# views.py
def list(request, nav_id):
    pass

除了支持int, django的url路由也支持str,slug,uuid,path四種類型, 一般常用的也就是str和int

3. 正則表達式路由
from django.urls import path, re_path

from . import views

urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
]

path函數(shù)定義的是普通的路由
re_path韓都定義正則路由, 參數(shù)完全一樣

像上面這個例子中的re_path, 每個()中就是一個參數(shù)的定義, ?P說明這里定義的是一個參數(shù), <year>是參數(shù)key, [0-9]{4}是正則表達式, $符代表路由結(jié)束, 不再往后匹配。
所以這個url可以匹配到articles/2018/ 這樣的url

urlpatterns = [
    re_path(r'^comments/(?:page-(?P<page_number>\d+)/)?$', comments),  # good
]

在這個例子中, ?: 代表是這是一個字符串的url, page-并不是一個參數(shù)
所以匹配的url是comments/page-1 的url

4. 包含其他的路由
from django.urls import include, path
urlpatterns = [
    path('account/', include('account.urls')),
]

include
include(module, namespace=None)
include(pattern_list)
include((pattern_list, app_namespace), namespace=None)

module: urlconf的模塊
namespace: url入口的命名空間
pattern_list: 可以迭代的path()/re_path實例類
app_namespace: url入口的app的命名空間

之后會依次講, 一般我們常見的也就是include('account.urls'))這種方式, 會將account/urls.py中定義的url路由都加載進來
一下就是在account/urls.py中定義的路由

# account/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('index/', views.index, name='account-index')
]

根據(jù)以上的路由規(guī)則, 我們可訪問的url就是account/index/ 這個url

定義會員注冊的路由

將模塊路由文件加載進項目中

# cms/urls.py
import debug_toolbar
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('account/', include('account.urls'))
]

這樣關(guān)于會員模塊的url路由, 我們就都可以在account/urls.py文件中定義了。

在模塊中定義url路由

創(chuàng)建account/urls.py文件

# account/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('register/', views.register, name='account-register')
]

View視圖模塊

視圖函數(shù)的定義

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

每個視圖函數(shù)都有一個固定的參數(shù)request, 這個是Request對象, 包含瀏覽器發(fā)起請求帶的參數(shù), 包括常見的url, post數(shù)據(jù), 請求類型, header頭等等。

然后視圖函數(shù)的返回也必須是一個Reponse對象, 一般我們返回的都是html代碼, 所以使用的是HttpResponse對象
有時候我們寫的是接口, 使用的是JsonResponse對象

視圖中最常用的函數(shù)

render 模板渲染函數(shù)

但是如果把html代碼寫在python文件中, 也太不好看了, 所以, 我們可以通過render函數(shù)來完成模板的渲染
render(request, template_name, context=None, content_type=None, status=None, using=None)

request: 是請求對象,
template_name: 模板文件名稱
context: 要傳遞給模板文件的變量
content_type: 文檔header頭中Content-Type的類型
status: http響應(yīng)碼, 就是200, 301, 302, 400, 500那個
using: 要使用的模板引擎

from django.shortcuts import render

def my_view(request):
    # View code here...
    return render(request, 'myapp/index.html', {
        'foo': 'bar',
    }, content_type='application/xhtml+xml')

render會自動幫我們轉(zhuǎn)換成HttpResponse對象, 所以也不需要再寫一遍HttpResponse了

redirect 跳轉(zhuǎn)函數(shù)

當(dāng)會員注冊完成后, 我們就需要自動跳轉(zhuǎn)到會員中心頁或者首頁, 這時就得使用redirect函數(shù)來實現(xiàn)了
redirect(to, permanent=False, *args, **kwargs)

to: 要跳轉(zhuǎn)的地址,
permanent: 是否永久跳轉(zhuǎn), 說白了就是301/302代碼的區(qū)別, 不動301,302自行百度。

from django.shortcuts import redirect
def my_view(request):
    ...
    return redirect('/some/url/')
reverse Url路由轉(zhuǎn)url函數(shù)

redirect完成url跳轉(zhuǎn)時, 萬一我定義的url都變了, 覺得以前定義的url太丑了, 太長了, 老板不喜歡了, 那不坑爹了么?那我不得滿項目找url, 挨個改阿?
其實django已經(jīng)想到這點了, 還記得我們在定義url路由時的name參數(shù)么?
通過reverse函數(shù), 就可以將urls.py中定義的url路由轉(zhuǎn)換為url了

reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
viewname: url路由的名稱
urlconf: url路由的模塊名, 默認是根模塊, 也就是咱們的cms文件中的urls.py
args: 要傳遞給url路由的參數(shù)

視圖中的Request和Response對象

一般發(fā)生請求后有兩種資源, 一種是請求的資源,是瀏覽器發(fā)送給服務(wù)器的資源, 包括請求的url, 頭, 傳遞的參數(shù), cookie什么的。
還有一種是返回的資源, 就是服務(wù)器發(fā)送給瀏覽器的資源。

HttpRequest對象

常用的屬性如下

屬性 說明
schemehttp 或 https
body請求的主體
path請求的路徑 account/register/
path_info請求的路徑
method請求方法GET,POST
encoding編碼類型
content_typeheader頭 的Content-Type
COOKIEScookie信息
FILES表單的file字段上傳的文件信息
METAheader頭信息
session保存session信息, dict結(jié)構(gòu)

常用的方法

方法 說明
get_host()127.0.0.1:8000
get_port()請求的端口
get_full_path()請求的全路徑
is_ajax()是否ajax請求
HttpResponse對象

常用的屬性

屬性 說明
content請求返回的資源內(nèi)容
charset編碼
status_code返回的http狀態(tài)碼
JsonResponse對象

JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_dumps_params=None, **kwargs)

data: 要返回的json數(shù)據(jù), 是dict結(jié)構(gòu)
encoder: 數(shù)據(jù)的轉(zhuǎn)碼類, 一般不需要更改
json_dumps_params: json_dumps函數(shù)

針對我們的項目, 就可以在cms/utils.py(沒有就創(chuàng)建)文件中定義一個通用的返回jsonResponse的函數(shù),

from django.http import JsonResponse

def return_json(code = 0, message = 'success', data = [], url=''):
    return JsonResponse({
        'code': code,
        'url': url,
        'message': message,
    })

模板層說明

模板文件路徑

默認模板文件路徑會在模塊名/templates中, 但是在一般的項目開發(fā)中, 都會把所有的模板放在一起, 所以我們需要重新定義模板的路徑

# cms/settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # 將templates目錄放在根目錄
            os.path.join(BASE_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

在settings.py 中TEMPLATES.DIRS中增加os.path.join(BASE_DIR, 'templates'), 模板文件的目錄就變?yōu)榱薱ms/templates

靜態(tài)資源文件路徑和訪問url修改

靜態(tài)文件路徑同模板一樣, 我們也需要修改到根目錄下

# cms/settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = ('static', )

模板常用標簽說明

一般模板的頂部、底部等很多地方都是一樣的, 所以我們可以定義一個布局html頁面, 將這些一樣的地方提取出來放在一起

# templates/layout.html
{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{% block title %} {% endblock %}</title>
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css'%}">
</head>

<body>
    {% block body %} {% endblock %}
</body>
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<script src="{% static 'js/layer/layer.js' %}"></script>
<script src="{% static 'js/utils.js' %}"></script>
</html>

load static 這個標簽是指加載static模塊, 只有加載了后, 才可以使用{% static %}來讀取靜態(tài)資源文件
block endblock 定義了不同的塊, 并且為每個塊進行命名
這樣假設(shè)我定義了一個會員注冊頁

# templates/account/register.html
{% extends 'layout.html' %}
{% block title %} 注冊 {% endblock %}

那么, layout.html中的{% block title %} {% endblock %}就會被替換成"注冊"

extends 標簽, 指定的就是加載layout.html這個布局頁面

定義注冊會員的表單

我們先在account/forms.py中定義表單RegisterForm, 因為之前已經(jīng)定義了一個AccountForm, 所以我們這個表單可以直接繼承AccountForm

class RegisterForm(AccountForm):
    # 設(shè)置場景是新增用戶
    scene = 'insert'
    class Meta(AccountForm.Meta):
        # 使用自定義的Form, 就必須指定fields or exclude屬性, 否則報錯
        # 注冊的時候我們不需要設(shè)置status, 字段, 所以排除掉。
        fields = ('account', 'password', 'email', 'phone')

注冊的時候, 一般需要輸入重復(fù)密碼, 所以我們多定義一個rep_password字段

class RegisterForm(AccountForm):
    ... 忽略代碼
     rep_password = forms.CharField(
        label='重復(fù)密碼',
        required=True,
        error_messages={'required': '請再次輸入密碼'},
       widget=forms.PasswordInput())

    def clean_rep_password(self):
        # 驗證兩次輸入的密碼是否一致
        # 因為在clean_password方法中, 已經(jīng)加密了cleaned_data['password'], 所以這里只能取data['password']
        if self.data['password'] != self.cleaned_data['rep_password']:
            raise ValidationError('兩次輸入的密碼不一致')

        return self.cleaned_data['rep_password']

定義視圖

在視圖中, 如果是GET請求, 我們則渲染表單, 如果是POST請求, 我們就執(zhí)行注冊用戶

GET 請求的代碼

from django.shortcuts import render
from .forms import RegisterForm

def register(request):
    form = RegisterForm()
        return render(request, 'account/register.html', {'form': form})

編寫模板代碼

我使用的是bootstrap前端框架, 大家可以下載了放在static文件夾中, 修正layout.html中的路徑

首先我們先將layout.html布局模板加載進來

# templates/account/register.html
{% extends 'layout.html' %}
{% block title %} 注冊 {% endblock %}

然后在block body部分, 寫入我們要渲染的表單

{% block body %}
<div class="container">
    <div class="row" >
        <form action="{% url 'account-register'%}" method="post" onsubmit="return post(this)">
            {% csrf_token %}
            <div class="form-group">
                <label for="{{ form.account.id_for_label}}">{{ form.account.label}}</label> {{ form.account}}
            </div>
            <div class="form-group">
                <label for="{{ form.password.id_for_label}}">{{ form.password.label}}</label> {{ form.password}}
            </div>
            <div class="form-group">
                <label for="{{ form.rep_password.id_for_label}}">{{ form.rep_password.label}}</label> {{ form.rep_password}}
            </div>
            <div class="form-group">
                <label for="{{ form.email.id_for_label}}">{{ form.email.label}}</label> {{ form.email}}
            </div>
            <div class="form-group">
                <label for="{{ form.phone.id_for_label}}">{{ form.phone.label}}</label> {{ form.phone}}
            </div>
            <input type="submit" value="提交" class="btn btn-success">
        </form>
    </div>
</div>
{% endblock %}

很多地方基本都是一樣的, {{ form.}} 中的form就是我們在view中傳過來的form表單類
form.account_id_for_label: 就是input的類
{{ form.account.label}}: 是顯示的表單字段的名稱
{{ form.account}}: 會直接生成一段input的表單字段代碼。

打開瀏覽器, 我們可以看一下效果
五 【用django2.0來開發(fā)】實現(xiàn)會員注冊功能

看起來樣式還不錯
現(xiàn)在, 我們就可以點擊提交嘗試一下了


五 【用django2.0來開發(fā)】實現(xiàn)會員注冊功能

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文名稱:五【用django2.0來開發(fā)】實現(xiàn)會員注冊功能-創(chuàng)新互聯(lián)
本文地址:http://bm7419.com/article32/ddhisc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、做網(wǎng)站、服務(wù)器托管、手機網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)