Configuring django-userena

夙愿已清 提交于 2019-11-29 04:01:12

You usually get a SiteProfileNotAvailable when Django can't find your profile. As documented in the "Storing additional information about users", you need to define AUTH_PROFILE_MODULE to point to the model of you profile.

As wunki has aptly pointed out, it's important to define the AUTH_PROFILE_MODULE in your settings.py file to point to your subclass of UserenaBaseProfile or UserenaLanguageBaseProfile. As discussed in the userena tutorial, these are usually placed inside of the models.py file of your newly created 'accounts' project.

However, I've found that the python manage.py runserver will fail if you have already provided AUTH_PROFILE_MODULE. If you have provided AUTH_PROFILE_MODULE and still receive a SiteProfileNotAvailable error (on every URL of your app), it may be that you failed to add 'accounts' to your list of INSTALLED_APPS in settings.py.

Smita K

Try following step:

  1. In your settings.py file, add ’userena’, ’guardian’, ’easy_thumbnails’ to your INSTALLED_APPS tuple.

  2. Then again 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)

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

  2. Now add accounts to your INSTALLED_APPS tuple in your settings.py file.

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

    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(_('favouritesnack'),max_length=5)  
    
  4. 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/'  
    

The ‘accounts.MyProfile’ in AUTH_PROFILE_MODULE refers to the app ‘accounts’ containing the model class MyProfile as we defined earlier. The 3 login/logout URL statements tell Django where to have the URLs for Django-Userena to work.

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

    (r'^accounts/', include('userena.urls')),  
    
  2. 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 = 'yourgmailaccount@gmail.com'  
    EMAIL_HOST_PASSWORD = 'yourgmailpassword'  
    
    1. Go to your Command Prompt shell and type:

      python manage.py check_permissions

run /accounts/signin/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!