I’m a new Django user and while researching for a user authentication app to plug into a part-time project that I’m working on, I was recommended Django-Registration and Django-Userena. Django-Registration seems to be the most widely used user authentication app while Django-Userena seems pretty comparable feature-wise though not as popular. At the end I chose Django-Userena because it seems to have more recent updates (and I suppose the nicely designed website gave the impression that the this app is very user-friendly).

I set about Django-Userena’s documentations and everything seems pretty straightforward. The next 2 evenings prove to be hard for me as a newbie. The documentations assumed a high working knowledge of Django (on where to paste the code given) and is not as clear and specific as I would like it to be.  But what is worse I found some code typos django import ommissions in the docs and it just causes the whole installation  to fail. It took hours of searching on Google/Stack Overflow/Github Issues and pasting together bits of information here and there to get Django-Userena to work for me.

I’ll now set out the steps I took in installing Django-Userena and how I believe the documentations can be more specific in helping newbies like me install and use Django-Userena.

To install, type “pip install django-userena” into your Command Prompt shell. You may run into a problem in installing easy_thumbnail (it’s a required dependency used by django-userena and is automatically installed using the pip command) because Python Imaging Library (PIL) is not installed on your computer. To fix this, simply install PIL here.

Then to configure Django-Userena, you need to edit your project’s settings.py file:

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)

3. 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’.

4. Now add  ‘accounts’ to your INSTALLED_APPS tuple in your settings.py file.

5. 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(_('favourite snack'),max_length=5)

Note how the original documents forget to mention the need to import two django modules from the first two lines causing many people to run into problem and get the following Django error message:

SiteProfileNotAvailable at /accounts/signin/
No exception supplied

*I initially thought the ‘_’ in the code is a typo causing the code to fail, but an email from Petar Radosevic from Bread and Pepper (the company that made Django-Userena open-source) pointed out that the ‘_’ is a Django convention for a shorter alias for the function ugettext() – a translation module for internalisation.

6. 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.

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

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

8. 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'

 9. Go to your Command Prompt shell and type:

python manage.py check_permissions

I believe after these steps, your Django-Userena app should be up and running at http://localhost:8000/accounts/signup. Good luck with your Django-Userena installation and I hope you do not go through as much pain as I did.

Before I part, to style your Django-Userena app, copy all the files in the userena/templates folder into your project’s /templates folder. This post gives a very good explanation on how to override a view from an external Django app.

And lastly, I forget to mention how much I like Django-Userena once I get it all up and running. You should totally try it for your Django project too! 🙂