So you have started up a shiny new Django project and probably gone through the getting started tutorial. Somewhere along that line you are shown how to get the “free” functionality of the Django admin pages, along with authentication into the admin section. The next logical step is to say “wow, that’s great I should add some authentication to the views I have created”. A quick Google search will inform you, good. fucking. luck.
So after a couple days of messing around with this I have finally cobbled together a solution. Sharing is caring and such:
First things first, lets edit your sites urls.py file to add in our login and logout pages by adding the following:
No by default these two urls will use django.contrib.autu.views for it’s login and logout functionality and as such it will attempt to load the following templates.
But since you have used the parameter:
You have now specified your own location and filename for the template. First off head over to your django_templates folder and create the accounts folder. Within it lets create the following files:
Ok we’re almost free, now we just need to specify what pages require user authentication. Thankfully this is a mercifully painless step. All that is required is to add the @login_required annotation to any view that you wish to secure, like so:
Edit the views.py file for the application in question.
Now when you head to the index location of your application you should be redirected to the login page, where you can use a username password that you have generated in the /admin/ section, or the initial Django administrator account. WOOO!