用戶注冊和用戶profiles
現(xiàn)有的用戶已經(jīng)可以登錄,登出幌陕,修改他們的密碼诵姜,以及當他們忘記密碼的時候重置他們的密碼。現(xiàn)在搏熄,我們需要構(gòu)建一個視圖(view)允許訪問者創(chuàng)建他們的賬號棚唆。
用戶注冊
先創(chuàng)建一個表單,供填寫用戶名心例、密碼等
from django.contrib.auth.models import User
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput, label='Password') # 新增加的表單字段
password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput) # 新增加的表單字段
class Meta:
model = User # 模型表單使用User模型
fields = ('username', 'first_name', 'last_name',) # 表單內(nèi)容使用元組內(nèi)的字段
def clean_password2(self):# 自定義的表單驗證宵凌,函數(shù)命名規(guī)則clean_<fieldname>,當通過調(diào)用is_valid()方法驗證這個表單(form)時這個檢查會被執(zhí)行止后。
cd = self.cleaned_data
if cd['password'] != cd['password2']:
raise forms.ValidationError('兩次密碼不同')
return cd['password2']
# 表單(forms)還包含了一個clean()方法用來驗證表單(form)的所有內(nèi)容瞎惫,這對驗證需要依賴其他字段的字段是非常有用的溜腐。
# Django還提供一個UserCreationForm表單(form)給你使用,它位于django.contrib.auth.forms非常類似與我們剛才創(chuàng)建的表單(form)
#表單(forms)還包含了一個clean()方法用來驗證表單(form)的所有內(nèi)容瓜喇,這對驗證需要依賴其他字段的字段是非常有用的挺益。
Django還提供一個UserCreationForm表單(form)給你使用三痰,它位于django.contrib.auth.forms非常類似與我們剛才創(chuàng)建的表單(form)
寫registration注冊視圖
...import省略
def register(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
if user_form.is_valid():
cd = user_form.cleaned_data#獲得字典
password = cd['password']#獲取password field
new_user = user_form.save(commit=False)# 獲取User實例new_user,不提交
new_user.set_password(password)#nwe_user對象設(shè)置密碼
new_user.save()#提交
return render(request,'account/register_done.html',{'new_user': new_user})#渲染到register_done
else:
user_form = UserRegistrationForm()
return render(request, 'account/register.html',{'user_form': user_form})#空表單渲染到注冊頁
在urls中配置
url(r'^register/$', views.register, name='register'),
html文件
//register.html
{% extends "base.html" %}
{% block title %}Create an account{% endblock %}
{% block content %}
<h1>Create an account</h1>
<p>Please, sign up using the following form:</p>
<form action="." method="post">
{{ user_form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Create my account"></p>
</form>
{% endblock %}
//register_done.html
{% extends "base.html" %}
{% block title %}Welcome{% endblock %}
{% block content %}
<h1>Welcome {{ new_user.first_name }}!</h1>
<p>Your account has been successfully created. Now you can <a href="{% url "login" %}">log in</a>.</p>
{% endblock %}
可以在login.html中添加注冊超鏈接
擴展User的方法
有兩種方式來擴展user模型丧荐,一種是繼承AbstractUser,重寫User類,還有一種方式佑钾,與django自帶的User模型進行OneToOne關(guān)聯(lián)(一對一關(guān)聯(lián))
為了保持你的代碼通用化肃续,當需要定義模型(model)和用戶模型的關(guān)系時黍檩,使用get_user_model()方法來取回用戶模型(model)并使用AUTH_USER_MODEL設(shè)置來引用這個用戶模型,替代直接引用auth的User模型(model)始锚。
from django.db import models
from django.conf import settings
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)#如果你沒有指定OneToOneField 的related_name 參數(shù),Django 將使用當前模型的小寫的名稱作為默認值喳逛。比如要反向查詢User對象的Profile 瞧捌,使用單個user.profile,如果有related_name,使用user.<RELATED_NAME>.
date_of_birth = models.DateField(blank=True, null=True)
photo = models.ImageField(upload_to='users/%Y/%m/%d', blank=True)#上傳圖片
def __str__(self):
return 'Profile for user {}'.format(self.user.username)
user一對一字段允許我們關(guān)聯(lián)用戶和profiles润文。photo字段是一個ImageField字段姐呐。你需要安裝一個Python包來管理圖片,使用PIL(Python Imaging Library)或者Pillow(PIL的分叉),pip安裝Pillow包典蝌。
為了Django能在開發(fā)服務(wù)中管理用戶上傳的多媒體文件曙砂,在項目setting.py文件中添加如下設(shè)置:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL 是管理用戶上傳的多媒體文件的主URL,MEDIA_ROOT是這些文件在本地保存的路徑骏掀。我們動態(tài)的構(gòu)建這些路徑相對我們的項目路徑來確保我們的代碼更通用化鸠澈。
現(xiàn)在,編輯bookmarks項目中的主urls.py文件截驮,修改代碼如下所示:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('account.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
在這種方法中笑陈,Django開發(fā)服務(wù)器將會在開發(fā)時改變對多媒體文件的服務(wù)。
static()幫助函數(shù)最適合在開發(fā)環(huán)境中使用而不是在生產(chǎn)環(huán)境使用葵袭。絕對不要在生產(chǎn)環(huán)境中使用Django來服務(wù)你的靜態(tài)文件涵妥。
之后,在admin中注冊以及數(shù)據(jù)庫遷移
創(chuàng)建表單給用戶可編輯
forms文件
from .models import Profile
class UserEditForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
class ProfileEditForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('date_of_birth', 'photo')
這兩個表單(forms)的功能:
- UserEditForm:允許用戶編輯它們的first name,last name, e-mail 這些儲存在User模型(model)中的內(nèi)置字段坡锡。
- ProfileEditForm:允許用戶編輯我們存儲在定制的Profile模型(model)中的額外數(shù)據(jù)蓬网。用戶可以編輯他們的生日數(shù)據(jù)以及為他們的profile上傳一張照片。
創(chuàng)建views文件
視圖文件
from .forms import LoginForm, UserRegistrationForm, \
UserEditForm, ProfileEditForm
@login_required
def edit(request):
if request.method == 'POST':
user_form = UserEditForm(instance=request.user,
data=request.POST)
profile_form = ProfileEditForm(instance=request.user.profile,
data=request.POST,
files=request.FILES)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
return redirect('dashboard')
#redirect使用文檔https://docs.djangoproject.com/en/dev/topics/http/shortcuts/
else:
user_form = UserEditForm(instance=request.user)
profile_form = ProfileEditForm(instance=request.user.profile)
return render(request,
'account/edit.html',
{'user_form': user_form,
'profile_form': profile_form})
創(chuàng)建url規(guī)則
url(r'^edit/$', views.edit, name='edit'),
最后鹉勒,在templates/account/中創(chuàng)建一個新的模板(template)命名為edit.html帆锋,為它添加如下內(nèi)容:
{% extends "base.html" %}
{% block title %}Edit your account{% endblock %}
{% block content %}
<h1>Edit your account</h1>
<p>You can edit your account using the following form:</p>
<form action="." method="post" enctype="multipart/form-data">
{{ user_form.as_p }}
{{ profile_form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Save changes"></p>
</form>
{% endblock %}
我們在表單(form)中包含enctype="multipart/form-data"用來支持文件上傳。我們使用一個HTML表單來提交兩個表單(forms): user_form和profile_form贸弥。
使用一個定制User模型(model)
Django還提供一個方法可以使用你自己定制的模型(model)來替代整個User模型(model)窟坐。你自己的用戶類需要繼承Django的AbstractUser類,這個類提供了一個抽象的模型(model)用來完整執(zhí)行默認用戶。你可訪問 https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#substituting-a-custom-user-model來獲得這個方法的更多信息哲鸳。
使用一個定制的用戶模型(model)將會帶給你很多的靈活性臣疑,但是它也可能給一些需要與User模型(model)交互的即插即用的應(yīng)用集成帶來一定的困難。
使用messages框架
一次性展示信息的框架
from django.contrib import messages
常用方法:
- success():當操作成功后顯示成功的messages
- info():展示messages
- warning():某些還沒有達到失敗的程度但已經(jīng)包含有失敗的風險徙菠,警報用
- error():操作沒有成功或者某些事情失敗
- debug():在生產(chǎn)環(huán)境中這種messages會移除或者忽略
html文件添加
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li class="{{ message.tags }}">
{{ message|safe }}
<a href="#" class="close"> </a>
</li>
{% endfor %}
</ul>
{% endif %}
views中添加
from django.contrib import messages
@login_required
def edit(request):
if request.method == 'POST':
# ...
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
messages.success(request, 'Profile updated '\
'successfully')
else:
messages.error(request, 'Error updating your profile')
else:
user_form = UserEditForm(instance=request.user)
# ...
創(chuàng)建一個定制的認證(authentication)后臺
Django允許你通過不同的來源進行認證(authentication)讯沈。AUTHENTICATION_BACKENDS(默認設(shè)置,并不在settings文件中顯示的顯示出來)設(shè)置包含了所有的給你的項目的認證(authentication)后臺婿奔。默認的缺狠,這個設(shè)置如下所示:
('django.contrib.auth.backends.ModelBackend',)
https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#other-authentication-sources
當你使用django.contrib.auth的authenticate()函數(shù),Django會通過每一個定義在AUTHENTICATION_BACKENDS中的后臺一個接一個地嘗試認證(authentication)用戶萍摊,直到其中有一個后臺成功的認證該用戶才會停止進行認證挤茄。只有所有的后臺都無法進行用戶認證(authentication),他或她才不會在你的站點中通過認證(authentication)冰木。
Django提供了一個簡單的方法來定義你自己的認證(authentication)后臺穷劈。一個認證(authentication)后臺就是提供了如下兩種方法的一個類:
- authenticate():將用戶信息當成參數(shù),如果用戶成功的認證(authentication)就需要返回True踊沸,反之歇终,需要返回False。
- get_user():將用戶的ID當成參數(shù)然后需要返回一個用戶對象逼龟。
創(chuàng)建一個定制認證(authentication)后臺非常容易评凝,就是編寫一個Python類實現(xiàn)上面兩個方法。我們要創(chuàng)建一個認證(authentication)后臺讓用戶在我們的站點中使用他們e-mail替代他們的用戶名來進行認證(authentication)
在應(yīng)用路徑下創(chuàng)建authentication文件
from django.contrib.auth.models import User
class EmailAuthBackend(object):
def authenticate(self,username=None, password=None):
try:
user = User.objects.get(email=username)
if user.check_password(password):
return user
return None
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
在settings中添加認證后臺
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'account.authentication.EmailAuthBackend',
)
這樣腺律,就可以在登陸中奕短,使用郵箱來登錄了。
為你的站點添加社交認證(authentication)
1.需要安裝python-social-auth模塊
2.在settings中添加應(yīng)用
INSTALLED_APPS = (
#...
'social.apps.django_app.default',
)
3.數(shù)據(jù)遷移
4.在主urls中設(shè)置
url('social-auth/',
include('social.apps.django_app.urls', namespace='social')),
5.為了確保社交認證(authentication)可以工作疾渣,你還需要配置一個hostname篡诽,因為有些服務(wù)不允許重定向到127.0.0.1或localhost。為了解決這個問題榴捡,在Linux或者Mac OSX下杈女,編輯你的/etc/hosts文件添加如下內(nèi)容:
127.0.0.1 mysite.com
這是用來告訴你的計算機指定mysite.com hostname指向你的本地機器。如果你使用Windows,你的hosts文件在 C:\Winwows\ System32\Drivers\etc\hosts吊圾。
為了驗證你的host重定向是否可用达椰,在瀏覽器中打開 http://mysite.com:8000/account/login/ 。如果你看到你的應(yīng)用的登錄頁面项乒,host重定向已經(jīng)可用啰劲。