Table Of Contents

This Page

Writing your first site, part 1

Throughout this tutorial, we’ll walk you through the creation of a web site dedicated to wine. If you are in a hurry and want to see facts and no words, the web site is called vinoweb and it is included in the examples directory.

We’ll assume you have django and djpcms installed already. You can tell djpcms is installed by running the Python interactive interpreter and typing import djpcms. If that command runs successfully, with no errors, djpcms is installed. You can even run the tests:

import djpcms
djpcms.runtests()

Where to get help:

If you're having trouble going through this tutorial, please post a message to djpcms-users.

Creating vinoweb

First thing is to create a new Django project. From the command line, cd into a directory where you'd like to store your code, then run the command:

django-admin.py startproject vinoweb

This will create a vinoweb directory. Move into the directory and create the application we will use in the site:

django-admin.py startapp vino

Great, your directory structure should look like this:

vinoweb/
  vino/
    __init__.py
    models.py
    tests.py
    views.py
  __init__.py
  appurls.py
  manage.py
  settings.py
  urls.py

Edit the vino/models.py file and add the django models for this application (cut and paste from djpcms/examples/vinoweb/vino/models.py file).

Setting up djpcms

Change into the vinoweb directory and edit the setting.py file and add at least the following applications to INSTALLED_APPS:

INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.sites',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.admin',
        'djpcms',
        'vino'
)

and add djpcms.core.context_processors.djpcms to TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.contrib.messages.context_processors.messages",
    "djpcms.core.context_processors.djpcms"
    )

Let's verify this worked:

python manage.py validate
0 errors found.

good.

Templates and media

Create a new directory templates and add template root.html file:

{% extends "djpcms/root.html" %}

and the base.html file:

{% extends "djpcms/base.html" %}

the djpcms/base.html is a battery included template for quick development. You can also use your own, but let's stick with this for now.

Edit the settings.py file and add the lines:

import os
basedir = os.path.split(os.path.abspath(__file__))[0]
TEMPLATE_DIRS = (os.path.join(basedir,'templates'),)

Let also create a media directory containing the static files. This directory will contains another directory called site and a cascade style sheet file file:vino.css. In your settings file replace the default media settings with these:

ADMIN_MEDIA_PREFIX = '/media/admin/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(basedir, 'media')

Urls

Last step of this first tutorial concerns the setting up of urls. First edit the urls.py which was created when we started the project and replace its content with:

from djpcms.urls import *
urlpatterns = site_urls.patterns()

Second, edit the settings.py and add the djpcms specific APPLICATION_URL_MODULE setting:

APPLICATION_URL_MODULE = 'vinoweb.appurls'

This module, which can be empty, will be imported by djpcms during the urls setup. We will add code further on in our tutorial.

Great, we've done a lot, your project should look like this:

vinoweb/
  media/
    site/
      vino.css
  templates/
    base.html
    root.html
  vino/
    __init__.py
    models.py
    tests.py
    views.py
  __init__.py
  appurls.py (empty)
  manage.py
  settings.py (modified)
  urls.py (modified)

Just one last thing, add the database information to the settings file, and run syncdb:

python manage.py syncdb

Ready to add contents? Go to the next tutorial.