django userena 使用

参考  http://docs.django-userena.org/en/latest/installation.html#required-settings

 

我用的是django1.9.7, userena2.0.1

原来文档的顺序跑不出来, 我跑出来的顺序是

1  

 pip install django-userena

 

2  

Required settings

You need to make some changes Django settings if you want to use Userena in your project. This means modifying AUTHENTICATION_BACKENDSINSTALLED_APPS and optionally MIDDLEWARE_CLASSES.

Begin by adding , guardian and easy_thumbnails to the INSTALLED_APPS in your settings.py file of your project. django.contrib.sites must also be present if it is not already (see Django docs.).

Next add UserenaAuthenticationBackend and ObjectPermissionBackend also in your settings.py file, from django-guardian, at the top of AUTHENTICATION_BACKENDS. If you only have Django’s default backend, adding django-guardian and that of userena will get the following:

AUTHENTICATION_BACKENDS = (
    'userena.backends.UserenaAuthenticationBackend',
    'guardian.backends.ObjectPermissionBackend',
    'django.contrib.auth.backends.ModelBackend',
)

add the following into your settings.py file:

ANONYMOUS_USER_ID = -1

 

3

Start New App

Next, you need to create a new app on your Django project. In your Command Prompt shell, type:python manage.py startapp accounts. We are creating a new app for Userena titled ‘accounts’.

Next, add accounts  userena to the INSTALLED_APPS in your settings.py file.

 

4

AUTH_PROFILE_MODULE = 'accounts.MyProfile'

To integrate Django with userena you should alter the following three settings to reflect the URI you have chosen for userena. For example, if userena lives under accounts:

USERENA_SIGNIN_REDIRECT_URL = '/accounts/%(username)s/'
LOGIN_URL = '/accounts/signin/'
LOGOUT_URL = '/accounts/signout/'

在accounts.models中加入

from __future__ import unicode_literals

from django.db import models

from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from userena.models import UserenaBaseProfile

class MyProfile(UserenaBaseProfile):
    user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=_('user'),
                                related_name='my_profile')
    favourite_snack = models.CharField(_('favourite snack'),
                                       max_length=5)

 

  6 接着url配置

在urls.py中加入

url(r'^accounts/', include('userena.urls')),

 

 

 7 email 配置好, smtp要自己去配置的,我用的是qq邮箱

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.qq.com'
EMAIL_PORT = 25
EMAIL_HOST_USER = '5479303@qq.com'
EMAIL_HOST_PASSWORD = 'hzppuevkhrqubdcd'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

SITE_ID = 1

 

8

python manage.py check_permissions

 

 

 

 

 

就可以注册用户了 http://localhost:8000/accounts/signup/

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值