I’ve been using Django for a while now for it’s built in admin system, it’s ridiculously easy to setup and create a working admin section for clients. If they want to have testimonials for example, I just add a model and a admin entry for it, sync the database and it’s ready for them to enter content. Every time up until this point I’ve used Django just for the admin of projects, the front-end was usually written in PHP or Ruby and just pulled data in.
A couple of days ago I started converting a client project from consisting of a Django Admin, a PHP front-end and a Ruby API into a single project using Django for everything. Once I started doing this I found that setting up the front-end in Django is extremely simple… unless you need to do any sort of user registration. Now since I haven’t used it for anything more than just an admin before, I might be missing a very important yet easy step to get registration working in no time. I tried a lot of searches to find a way to setup user registration, but I couldn’t find anything simple; all I wanted to do was add a user the Auth module’s user table and just have them not have access to the admin. All I could find were sites saying to use the Django-Registration package or seemed to jump past some parts in using the included auth module.
After a full day of trying to figure things out I caved and forced myself to use the registration package, I had looked into it earlier but the tutorials were all confusing to me. I finally found one that made sense by Michele Pasin, in which setting up the registration package, the views and the SMTP debugging server are discussed. I followed Michele’s post to the letter and got the entire process working and all of the templates styled to match my project. There was only one thing left that I needed to do, collect the user’s first and last name. It’s not exactly helpful when you’re selling tickets to someone and you don’t know who they are.
This is where I ran into a problem, the registration package doesn’t include a way of registering the user’s first and last name; it doesn’t make sense to me as this data is all stored in the default auth module’s user table which has those two fields. After searching for an hour or so and only coming across sites saying to use the Django-Profiles package along with it I decided to roll my own form that handles it. The Django-Profiles package is a really great tool when you want to collect other information from your users, but it doesn’t make sense to use it for first and last name as the auth module already has those defined and why add another table containing information that should already be handled.
It took me about another hour to get this working and I’m going to detail it here for you so hopefully it helps you (more than likely though it’ll just be a reminder to myself of how to do it).
In my project the app where this is going to live is called shop, you can put this in whichever app makes the most sense in your project.
The first thing we need to do is edit our projectsurls.pyfile and add this line directly above the one that includes the Django-Registration urls.
Next we need to setup the form that’s going to be used. Create a file calledforms.pyand then enter the following into it:
Once the form is complete we need to need to update ourviews.pyfile, just make sure the following is in there somewhere, you can edit it to match your coding style but the important part is where we use ourProfileForm.
Since I couldn’t seem to find when django-registration actually uses theactivation_complete.htmlfile that’s where I decided to put our customProfileForm. Just edit the existing one to match your project’s style and add the following to it.
Now here’s the important part, you need to edit yourlogin.htmlfile’s opening form tag to the following:
This makes sure the user is actually directed to our work after logging in. When they login until they supply a first and last name they will be presented with the form, once they fill in the information they will always be redirected to the homepage (unless you change the redirects in theviews.pyfile).