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 = '[email protected]'
EMAIL_HOST_PASSWORD = 'hzppuevkhrqubdcd'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

SITE_ID = 1

 

8

python manage.py check_permissions

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

猜你喜欢

转载自wzgdavid.iteye.com/blog/2310816