Advertisment

Enhancing Websites With Django

author-image
PCQ Bureau
New Update


Advertisment

Advertisment

Sufyan bin Uzayr, Freelance Writer, Graphic Artist, Photographer, www.sufyan.co.nr

Snapshot

Price:Free

Applies to: Web developers, Python programmers

USP:An open source and free Python framework to develop websites



Related articles: Create GUI Apps in Python Using Qt Designer http://ld2.in/3xs
Search engine keywords: django, python, framework, web development

Django is a free, open source framework based on Python that can be used to develop websites and define complex data structures. In Django, applications consist of separate models (description of data), views (generation of data to be displayed) and templates (visual appearance). Further more, you can easily define model classes for your data. Once your app has been setup, an admin panel is created out of the box and you can customize it at leisure.

Advertisment

Installation

If you are using Linux, chances are you already have Python installed. If not, you can get it from your distro's repositories. For instance, Ubuntu users can use:

sudo apt-get install python-django

Advertisment

If you are using Windows, you can download Python from the website www.python.org/download. Once extracted, you will need to point to the folder before proceeding.

Next, head to www.djangoproject.com and download the archive. Django is licensed under a BSD license and comes with an in-built SQLite database and so if you do not have a production environment, you can still experiment with it.

Advertisment

Implementation

Now that we have installed Django, let us create a simple application that lets you keep a log of your transactions. In order to create such an app, we need to store a list of funds and expenditure, and then associate a given amount of money with a particular expenditure as and when needed. Naturally, both 'funds' and 'expenditure' shall have their own associated sub-fields.

Creating the site

Advertisment

First, use the command line to navigate to the folder where you wish to keep this project and then type:

python django-admin.py startproject pcqname

This will create the website's basic structure within the folder 'pcqname'. Now, navigate to that folder ('pcqname'). Note, however, that Windows users might need to pass the absolute path the django-admin.py with C:\Python27\Scripts\django-admin.py.

Advertisment

Creating the app

The site that we created above now needs an application which shall contain most of the code. The same app can be used over multiple sites and vice versa. In order to create an app, use the command:

python manage.py startapp pcqcntct

Setting up the database

In the project folder, open the file named 'settings.py' (you can use any standard text editor such as Notepad). In the starting of the file itself, you will find a configuration line of code called DATABASES, which will be set to 'default'. Under that, change the ENGINE value to django.db.backends.sqlite3 and the NAME value to anything of your choice (say, 'mydjangoprj.db'). The NAME value is the path to the database.

Adding the app to the site

To add your app the site, go to the INSTALLED_APPS value under the same configuration line of code and add the name of the app right at the end.

Activating the admin panel

In order to activate the Admin Panel, first uncomment the line django.contrib.admin by removing the # character before it (within the same 'settings.py' file). Next, open the 'urls.py' file in the same directory and uncomment the admin lines (the instructions and locations of such lines are mentioned in that file itself).

Creating modules

Module creation is a fairly simple process. Just open the 'models.py' file and add class definitions as follows:

class CLASSNAME()

#variable-definitions to follow

class CLASSNAME()

#variable-definitions to follow

class CLASSNAME()

#variable-definitions to follow

Additionally, you may also need to add from django.db import models if it already does not exist in the file.

If you wish to make certain fields optional in the above class definitions, you can use the blank=True or null=True parameters. Similarly, you can use the __unicode__ function for string variables. Once you add the class definitions, the data structure of the site is ready. Now, just create an admin.py file and start setting up objects in Admin Panel.

Running syncdb

Use the command line to navigate to the project folder and issue the following command:

python manage.py syncdb

This will generate the database and the basic structure required by each app. You will be asked to enter the login credentials (name, email, password) for the administrator. Note that, however, if you modify your app at a later date, you will have to delete your database file and re-run the above command.

Starting the server

From the command line (within the project folder), run the following command:

python manage.py runserver

As per default settings, it will launch a local server (good enough for testing, definitely not usable for productivity). The server is set to automatically restart if you change the access code, but it cannot notice any changes that you make in project files. So, if you save any file with error(s), the server might stop working.

Using the site

You can access your website in any web browser installed locally and login using credentials that you chose earlier.

With that, we come to the end of this brief Django tutorial. Since this tutorial covered usage of Django only to set up a personal log manager app and site,

we have created only the admin panel, not the front end.

If you wish to further explore Django, you can learn

more about it at http://docs.djangoproject.com Do

write to us with your experiences of coding with Django at pcquest@cybermedia.co.in.

Advertisment