1 February 2008

Online games portal in Django - tutorial, part 1

To practice my Django skills and to give you some code showing how easy is to create CMS like apps in Django, I'm going to create simple online games portal. Here is my plan:

  • setup environment
  • create model for multisite game portal with games, subcategories and categories
  • enhance it to get use of Django admin
  • create population mechanism to get the data
  • create mash-up, HTML with simple CSS
  • create url patterns and views (with max usage of generic views)
  • create templates; end of first complete version
  • modify and populate model for multilingual portal (Polish version)
  • modify everything else (urls, templates, settings) to get two separate portals: Polish and English
  • because portal is very static (no users, no sessions except search and hit counter), I'm going to create static pages generator

Let's start! Setup I'm not going to create production environment, because it is all about practicing, so Django development server should be ok. Currently I'm developing on Windows XP but if you have Ubuntu 7.10 apt-get can give you most of the things you want. What we need (I'll enumerate my setup, but in many cases you may use other, for example older python, other editor etc.):

More information about setting up Django read this.

Create project games4all using command:

python django-admin.py startproject games4all

Then enter the created directory games4all and run command:

python manage.py startapp gampor

Django application was created. Django applications are loosely coupled with projects, so can be moved around really quickly. All that setting up created several files and we are now interested in one of them: settings.py. It contains all project setting and we want to change some of them. Locate below elements and change them accordingly:

DATABASE_ENGINE = 'sqlite3' # For development sqlite database will suffice.
DATABASE_NAME = 'games4all' # Name of database file.
USE_I18N = False # Because site will be only in English (at least for now), disable I18N.
MEDIA_ROOT = 'public_html/' # This directory will contain media files, also create this directory in games4all folder.
MEDIA_URL = 'http://localhost:8080/m/' # Url for media files (for development usage).
ADMIN_MEDIA_PREFIX = 'http://localhost:8080/media/' # (Admin media files URL).
INSTALLED_APPS = ( # Add two last applications: admin site and our game portal.
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'games4all.gampor',
)

Save file and try if everything works. First run:

python manage.py syncdb

This will create the database tables necessary to run Django contributed applications. Of course our game portal models still do not exist but we will go to that later. If there were no errors, we can try to run admin site, but first we must edit one file - urls.py - to tell where admin should be available. Open it and insert below code:

from django.conf.urls.defaults import *
from django.conf import settings

urlpatterns = patterns('',
(r'^admin/', include('django.contrib.admin.urls')),
)

if settings.DEBUG:
urlpatterns += patterns('',
(r'^m/(?P.*)$', 'django.views.static.serve', {'document_root': 'public_html'}),
(r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': 'E:/django_contribs/django-trunk/django/contrib/admin/media'}),
)

Please change the last path to the one where you have Django trunk. Because we will use development server, we must also serve static files using it (if settings.DEBUG part). First part of file enables admin site. Now we must start development server to see if everything works:

python manage.py runserver 8080

If server starts OK, open browser and use URL http://localhost:8080/admin/ to see admin site. Log in and look around. So much and so little code! This will be the end of part one. Tomorrow I'm going to show how to create simple model for game portal...

2 komentarze:

  1. Those people behind every Download Games are so amazing. Developers often think of how to make it almost perfect. This site is so educational that gives and teaches us to be aware of the source codes, just to give us a bit of information on how games works...

    ReplyDelete
  2. Thanks for sharing this tutoriol, I thought it would be hard to create Video Games portal in Django.

    ReplyDelete