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

Python的Django中django-userena組件的簡單使用教程

 更新時間:2015年05月30日 16:51:15   投稿:goldensun  
這篇文章主要介紹了Python的Django中django-userena組件的簡單使用教程,包括用戶登陸和注冊等簡單功能的實現(xiàn),需要的朋友可以參考下

利用twitter/bootstrap,項目的基礎模板算是順利搞定。接下來開始處理用戶中心。

用戶中心主要包括用戶登陸、注冊以及頭像等個人信息維護。此前,用戶的注冊管理我一直使用django-registration。只是這個APP有些不思進取,09年發(fā)布了0.8alpha版后就一直沒什么動靜。這次決定嘗試另外一個用戶模塊組件django-userena

相比django-registration,django-userena的功能要完善的多。除基礎的登陸注冊模塊外django-userena甚至還帶了站內消息功能。django-userena的易用性方面也做的非常的不錯。django-userena自帶了默認模板,并有提供一個完整的演示項目,讓你可以輕松上手。這里有個官方的在線demo,感興趣可以去看看。
django-userena同twitter/bootstrap的整合

我們自然是希望所有的APP不用做任何修改,拿來就能用了。不過事與愿違,在整合的過程中多多少少都會遇到一些問題。django-userena默認的模板在項目中顯示的非常難看。我們需要重寫django-userena的默認模板,并且用django-bootstrap來生成form。

forms.py
#為原始form添加BootstrapMixin
from bootstrap.forms import BootstrapMixin

class BsAuthenticationForm(AuthenticationForm, BootstrapMixin):
 def __init__(self, *args, **kw):
  super(BsAuthenticationForm, self).__init__(*args, **kw)
  self.__bootstrap__()
 
urls.py
#重寫urls,指定使用的form
from django.conf.urls.defaults import *
from userena import views as userena_views
from profiles.forms import BsSignupForm, BsAuthenticationForm

urlpatterns = patterns('',
 url(r'^signup/$', userena_views.signup,
  {'signup_form': BsSignupForm}, name='userena_signup'),
 url(r'^signin/$', userena_views.signin,
  {'auth_form': BsAuthenticationForm}, name='userena_signin'),
 (r'^', include('userena.urls')),
)

中文用戶名問題

同django-admin一樣,django-userena也無法使用中文進行注冊。對于一個中文網(wǎng)站而言,不能使用中文注冊ID似乎有些太不合理的。

django-userena使用正則表達式對用戶名進行校驗,重寫注冊form修改認證規(guī)則即可取消該限制。

USERNAME_RE = r'^\S+$'
attrs_dict = {'class': 'required'}

class BsSignupForm(SignupForm, BootstrapMixin):
 username = forms.RegexField(regex=USERNAME_RE,
        max_length=30,
        widget=forms.TextInput(attrs=attrs_dict),
        label=_("Username"),
        error_messages={'invalid': _('Username must contain only letters, numbers, dots and underscores.')})

 def __init__(self, *args, **kw):
  super(BsSignupForm, self).__init__(*args, **kw)
  self.__bootstrap__()


相關文章

最新評論

延庆县| 黄大仙区| 石河子市| 西和县| 上虞市| 郎溪县| 象山县| 济阳县| 太白县| 汽车| 澄江县| 沁源县| 古交市| 乌什县| 鹰潭市| 望奎县| 延寿县| 辉南县| 扶沟县| 平昌县| 泰来县| 蓬溪县| 磐安县| 长子县| 炉霍县| 香港| 平顶山市| 万年县| 北流市| 双牌县| 横山县| 舒兰市| 大石桥市| 响水县| 太谷县| 本溪市| 香港| 濮阳县| 平罗县| 长武县| 玉树县|