While working on my bridge website (at this moment, very much in alpha state), which is written in Django, I had arrived at the point where I wanted users to be able to register themselves, instead of me having to add test users manually.
So I looked at Django’s user authentication documentation online. I was surprised that there wasn’t anything about registration. Apparently, I had to find another way to do that. Obviously, I’m not the first person to be in this situation so there had to be a module or plugin for Django that does user registration. And indeed there are. But which one to use?
Since I’m an IRC user, it was logical to visit #django on freenode and ask this question. Almost immediately, someone replied to use django-allauth, and nobody disagreed. The django-allauth page looks good: recent activity and extensive documentation.
I typed pip install django-allauth
in my virtual environment and started to change my code to add user registration:
- Edited settings.py
- Edited urls.py
- Ran manage.py syncdb
- Did not do the Site object steps, because initially I don’t want social app integration
When I visited my development site after that, the login and logout pages had been replaced by django-allauth’s version, and logging in with existing users worked. So far, so good!
I noticed that, by default, the login page redirects to /accounts/profile/
after logging in successfully. This page did not exist and gave a “page not found” error. At first, I thought that I had done something wrong with configuring django-allauth, but after reading the FAQ, I learned that this is by design. Profile management is not provided by django-allauth; you have to do that some other way.
When trying to register a new user, and filling in all fields on the registration page, I got the following error:
Uh, what? Connection refused? Ah, I guess sending of the registration confirmation email failed, because I don’t have a mail server running on my development server. The FAQ of django-allauth mentions this as well. It suggests to add the following line to settings.py
:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
I did, and after that, registration worked. The registration email was not sent but printed in the django log.
The last thing to do, was to get the login and logout pages in the style of my website. This was not explained in the django-allauth documentation, but searching for an answer made me realise that this is done the same way as overriding other templates:
cd ~/venv/bridgedb/lib/python2.7/site-packages/allauth/templates/ cp account/base.html ~/src/bridgedb/templates/account/ cp account/login.html ~/src/bridgedb/templates/account/ cp account/logout.html ~/src/bridgedb/templates/account/
Then I edited the copied templates in ~/src/bridgedb/templates/account
and changed them to match the rest of my pages.
In doing this, I noticed that if you use the template base.html
as base for all your pages, and {% block content %}
as content block, the django-allauth pages are styled correctly without any effort. But I was using skeleton.html, so that didn’t work for me.
All in all, pretty easy to get this to work. Thanks, Raymond, for this module!
3 Responses to User registration in django with django-allauth