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
:
1 |
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:
1 2 3 4 |
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!
Hi joost,
How can we add we make allauth to send confirmation email to the user’s email rather than printing on the log. Is there a possibility using allauth or should we use another library to exclusively send email
Thanks
Hi Siva,
unfortunately I don’t know the answer to that question off-hand. I recommend you read through the docs of allauth. Probably you can configure either an SMTP server or something like a gmail account in its settings.
Good luck!
Hi Joost,
I am trying to login user with Google (already registered through inbuilt signup form). Now every time I try it I will get redirected to signup form, with username and email. If I click the form a new user will be created instead of logging in the current user.
Is there any way to prevent this ??