Hi folks.
So last time, we tried to implement a little RPG of mine using Meteor, which failed miserably. This time we are going to try and use Django. Let’s have at it, shall we?
If you don’t know what django is (than you are probably not reading this blog entry anyways. ), have a look => Link. It’s a Python Web Framework.
In the beginning
So here we are again. I’m starting off by creating the skeleton for my RPG. First, the welcome page with a tiny login capability. Which means session tracking. For now, on the same page.
To create the skeleton I just run this little command:
django-admin.py startproject myrpg
Easy enough, right? Basic directory structure is created. You can read that in django’s own documentation: https://docs.djangoproject.com/en/1.8/intro/tutorial01/
I’m using Sublime Text 3 to build this app. I was thinking of pycharm, but that would take away too much fun.
By default Django uses SQLlite and I’m okay with that.
The basics are there. Let’s move to higher ground. The next step can be to design the login page. I’m not much of a designer so I’ll leave that part to people who care more / understand it better. I’m only looking for a simple Login with a username and a password. Nothing fancy. And I’m going to use Django’s auth system for users: django.contrib.auth.
It’s pretty good, has lots of features and can auth a user pretty quickly given a username and password, which is all I want for now.
I running a migrate command to create all the necessary tables and data:
python manage.py migrate
A quick check with sqlite3 and running **.schema **showed me that the tables are indeed created and filled with default data for all the **INSTALLED_APPS **django has at the moment.
I did a quick check with python manage.py runserver and everything seems to work fine.
The RPG App
So, that concludes the setup. Following the tutorial, django has this notion of apps and projects. In short, a project is a set of configurations and applications which forms a website. Hence, I’m going to create an RPG APP for now. The tutorial continues with a Poll type of App, but I’m sure I can apply the same things to a login page.
First, let’s create the App using the command:
python manage.py startapp rpg
That creates the skeleton for the app. The tutorial then suggests to define the database first. Which I think is a bad idea; usually when doing design, you don’t want to start with the database model. But for now, I shall comply. We want a login page and we are using the auth module, which means, for now, I don’t care about the database. Skip.
I’m going to go off corse for now, since I only want to create a basic login. For that, I have to read up on views and models a little bit in order to create a simple login page. Be right back.
I’m fighting the basic auth system at the moment. Everything seems to be working fine. In the Admin screen I was able to add a basic user to do some debugging, but for now, the login seems to not work with this error:
CSRF verification failed. Request aborted
This is the CSRF protection which came out with Django. I did a bit of more document reading and setup the CSRF as was described here:
https://docs.djangoproject.com/en/1.8/ref/csrf/
However, I’m still facing the same problem. Let’s research further. Ahh yes. I wasn’t returning the request context I’ve set up. Before:
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login
from django.template.context_processors import csrf
def login_user(request):
c = {}
c.update(csrf(request))
state = "Please log in below..."
username = password = ''
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response('auth.html',{'state':state, 'username': username})
After:
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login
from django.template.context_processors import csrf
from django.template import RequestContext
def login_user(request):
state = "Please log in below..."
username = password = ''
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response('auth.html',{'state':state, 'username': username}, context_instance=RequestContext(request))
Notice that now I’m returning the request context at the end when I’m rendering the response. And lo’ and behold, my login is now working.
Progress so far
So, I’ve set up a user using the Admin portal accessible from the app / admin. Created my default rpg app, and added my default auth view into a template folder. Configured the URL setting in urls.py to look like this:
url(r'^login/$', 'rpg.views.login_user'),
Very basic for now, but when I return to it, we are going to clean it up and then continue with implementing the main screen which will greet the user and display some stats about his/her character.
The whole code is under github here => https://github.com/Skarlso/myrpg
Bye for now,
And thanks for reading.
Gergely.