For marketers
who love technology
Home » , , » A Django tutorial on Debian - part 5: implementing the frontend

A Django tutorial on Debian - part 5: implementing the frontend





Django logo
Django logo (Photo credit: Wikipedia)

In Django the essential part of any project consists in defining the data structures. Then, implementing a webpage to a web application consists in nothing more than providing a "view" on these data structures. We will define such view for our web application in the file textModif/views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at textModif's index.")

def detail(request, textTask_id):
    return HttpResponse("You're looking at textTask %s." % textTask_id)

We also have to define a URL so that the servers knows when to display that view. So we create the textModif/urls.py file:

from django.conf.urls import patterns, url

from textModif import viewsurlpatterns = patterns('',
    url(r'^$', views.index, name='index')
    # ex: /textModif/5/
    url(r'^(?P\d+)/$', views.detail, name='detail'),
)

and we include the app's urls in the site's urls.py configuration:
    # load the URLs of the web app textModif
    url(r'^textModif/', include('textModif.urls'))

Now if we load the page "http://127.0.0.1:8000/textModif/" we will see the text "Hello, world. You're at the textModif index.". 

The whole role of a view is to receive an object that describes an HTTP request (request) and to do things on it, until we want to reply with an object decribing and HTTP reply (HttpResponse). We will define a template for our index page. The index page will just be a form to create a new textTask and submit it to a function that modifies the submitted text. The index will provide the reference of the created textTask. The detail page provides the results of the text transformation and all the details about the applied text modifications.

Let's create our template. First we have to edit settings.py to define the location of our templates' directory:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    /home/alan/virtualenv_1/djangoProj_1/mysite/template
)

Then we create this template:


mkdir template
touch template/index.html
sudo apt-get install bluefish
bluefish template/index.html &
TO BE CONTINUED




SHARE

About Gilles

0 comments :

Post a Comment