Friday, February 10, 2012

Checklist for enabling Authentication in Django

1. Ensure that following session and authentication middleware exists in settings.py
'django.contrib.sessions.middleware.SessionMiddleware'
2. Ensure that following session and authentication apps exists in settings.py
'django.contrib.sessions'
3. Add paths for login and logout in urls.py (the paths can also be different from those shown below)
('^accounts/login/$', login),
('^accounts/logout/$', logout),
4. Create html pages for login and logout at registration/login.html and registration/loggout_out.html
5. Be sure to put the csrf protection tag inside the login form
6. Be sure that you have at least one user account created (which you most likely will have at this point)
7. Add the login_required decorator to any view function
8. If you need to access the request object in your templates, add TEMPLATE_CONTEXT_PROCESSORS to settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
)

Checklist for enabling the Admin interface in Django

1. Make sure you have the following apps in INSTALLED_APPS
'django.contrib.admin'
'django.contrib.auth'
'django.contrib.contenttypes'
'django.contrib.sessions'

2. Make sure you have the following in MiddleWare
django.middleware.common.CommonMiddleware'
'django.contrib.sessions.middleware.SessionMiddleware'
'django.contrib.auth.middleware.AuthenticationMiddleware'

3. Add the admin site to URLConf
4. Ensure urls.py imports admin
5. Ensure autodiscover is on in urls.py
6. Add your models to the admin site by updating admin.py in your app directorie(s)
admin.site.register(Publisher)
...
7. python manage.py syncdb
8. Create the superuser when asked
9. Run the website and check

Checklist for creating models in Django

1. Create an app
2. Add model class to models.py in the app
3. Add the app to installed apps
4. Validate your model
python manage.py validate
5. Check the SQL which will be generated
python manage.py sqlall app_name
6. Create the tables in the db
python manage.py syncdb

Checklist for enabling databases in Django

1. Update the DATABASES section in settings.py with details of your database
2. Test the connection
>>> from django.db import connection
>>> cursor = connection.cursor()

Wednesday, October 5, 2011

LPTHW - Exercise 52

This is the last exercise in the LPTHW book. In this exercise we will create a web based game engine for the Gothon game (the one we created in exercise 42), using the structure we created in exercise 47, along with tests and everything.

The first thing I did was to copy the entire contents of ex47 to ex52. Then I renamed game.py to map.py and made a corresponding change for the test case also. Then I ran 'nosetests' and everything seems to be fine at this stage.

In exercise 42, each room was a function in the Room class. But now in exercise 52, we will make each Room an instance of the Room class. I coded the class map.py (containing all the rooms) as shown in the book. Next, I am going to copy the map_tests.py file as is in the tests directory.

After copying the code in map.py, I created another Python module for messages, called messages.py. This module will contain the initial text for all the rooms, as well as the transition text, which is displayed to the user when they transition from one room to another.

Next, I refactored map_tests.py to test all the rooms, their initial text, and transitions.

The book then explains how lpthw.web maintains sessions. I understand how sessions are managed, but the session variable which seems global to the module confused me, because it makes me think that only one instance of that variable would exist.

I copied the apps.py file in bin, and also fixed a couple of bugs.

I also added two HTML templates - layout.html, show_room.html, and game_lost.html

Part of the code is embedded below:

LPTHW - Exercise 51

In this exercise, we learn how to deal with form input and a bit about testing web apps.

The first thing I did was to change app.py and refactor it to handle web input. We will handle simple input as GET input parameters to start with.

Here is the new app.py file:



Now if you notice, in this file we use the line

form = web.input(name="Nobody")

to handle the input. If the input contains a 'name' parameter, then we will use it, else it will default to "Nobody", since we have given a default value as an argument to the function call.

Getting the values as url parameters is not a good idea:

This is a good beginning, but we should really be getting the values in a form and not as command line parameters. For this we will need to add some HTML and a method in the Index class to handle POST data (since this is the default method for sending form data).

I made a few changes to app.py, index.html, and added a new file hello_form.html All the code is embedded below:



Don't repeat boilerplate code:

It's very good that we can now handle form data. However, the observant programmer must have noticed that both index.html, and hello_form.html have common boilerplate code. It would be good if we can remove the repetition. Well, we can, if we use templates.


All the code which uses templates is embedded below:

LPTHW - Exercise 50

In this exercise we will make a web application from the Gothons game, which we made in exercise 46. To simplify the process of making a web application, we need a we need a web framework. For this exercise we will use lpthw.web, which is the same as the webpy framework.

I installed the web framework thus
pip install lpthw.web

Then I created a project for the web application by creating a directory in my projects directory:
ex50
/bin
app.py
/docs
/gothonweb
__init__.py
/templates
__init__.py
/tests

I just copied the code provided in LPTHW in app.py

I ran the webserver with 'python bin/app.py' and successfully loaded the url http://localhost:8080

However, I realized that lpthw.web was not installed in my virtualenv as I would have expected it to be. It was actually installed in /usr/local/lib/python2.6/dist-packages/ This seems strange and incorrect to me. I am not quite sure what went wrong, so I asked a question on StackOverflow.

The code for this exercise is embedded below: