userena - WBowam/wbowam.github.com GitHub Wiki

Date: 2013-09-14 Title: Django-userena 的使用 Tags: Django,userena Category: It


先Clone Github上的源码,运行Demo看看效果,

进入demo目录
 pip install -r requirements.txt
(再装一个html2text)
pip install html2text
python manage.py runserver

然后再自己搭一个,如下步骤


安装

pip install django-userena

配置

1. add ’userena’, ’guardian’, ’easy_thumbnails’ to your INSTALLED_APPS tuple.

’userena’,  
’guardian’,  
’easy_thumbnails’,

2. in your settings.py file, add the following:

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

ANONYMOUS_USER_ID = -1

The above is used to get django-guardian working (another Django-Userena dependency that’s automatically installed to control permissions)

3. create a new app for your Django-Userena app named 'accounts'.and add it to your INSTALLED_APPS tuple in your settings.py file.

4. Copy the following into accounts/models.py:

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)

5. Next add the following into settings.py file :

AUTH_PROFILE_MODULE = 'accounts.MyProfile'

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

6. Add the following into urls.py under the ‘urlpatterns’ tuple:

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

7. Configure your Django SMTP email settings to use Gmail in settings.py:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'yourgmailpassword'

8.Configure your Media files to use mugshots

settings.py

##added by Tulpar,20140514
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'public/media/')
MEDIA_URL = '/media/'

urls.py


##added by Tulpar,20140514
from django.conf import settings

urlpatterns += patterns('',
    url(r"^media/(?P<path>.*)$","django.views.static.serve",{"document_root": settings.MEDIA_ROOT,}),
)

That's All

##Errors

1.```IOError at /admin/account/myprofile/add/

decoder jpeg not available```

错误原因是Pilow的jpg图片支持组件没有安装

1、先卸载安装的Pillow
pip uninstall Pillow
2、安装JPEG和FREETYPE2库

sudo apt-get install libjpeg-dev  
sudo apt-get install libfreetype6-dev

3.重新安装Pillow

pip install -I pillow

2.no such table: easy thumbnails source

解决方法:

1.installed app里加

    #added by Tulpar,20140601
    'easy_thumbnails'

2.settins.py里加

##added by Tulpar,20140601
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'public/media/')
MEDIA_URL = '/media/'

后台使用django默认的admin时,userena已经默认给你register好了

###后台如果是使用Xadmin,就得自行register一下,如: adminx.py

#-*- coding: UTF-8 -*- 
#from django.contrib import admin

# Register your models here.
import xadmin

from models import MyProfile

class MyProfileAdmin(object):
	#search_fields=('name','category','content')
	#prepopulated_fields = { 'message': ['name'] }##learned at  http://www.b-list.org/weblog/2008/dec/24/admin/
	#exclude = ('created_by',)
	#actions = [Songda, ]
	list_display = ('user','favourite_snack')
	list_display_links = ('user',)
	ordering = ("-user",)
	list_filter=('user',)#该属性指定可以过滤的列的名字, 系统会自动生成搜索器
	search_fields=('user',)#属性指定可以通过搜索框搜索的数据列的名字, 搜索框搜索使用的是模糊查找的方式, 一般用来搜素名字等字符串字段
	list_export = ('xls', 'xml', 'json')#该插件在数据列表页面提供了数据导出功能, 可以导出 Excel, CSV, XML, json 格式.
	# 这会显示一个下拉列表, 用户可以选择3秒或5秒刷新一次页面.
	refresh_times = (3, 5,500)
	list_editable = ('favourite_snack')
	show_detail_fields = ['user',]#该插件可以在列表页中显示相关字段的详细信息, 使用 Ajax 在列表页中显示.

xadmin.site.register(MyProfile,MyProfileAdmin)
⚠️ **GitHub.com Fallback** ⚠️