Django, a prominent member of new generation web framework lets you build deep, dynamic, interesting sites in an extremely short time. Django is designed to let you focus on the fun, interesting parts of your job while easing the pain of the repetitive bits. Django provides high-level abstractions of common Web development patterns, shortcuts for frequent programming tasks, and clear conventions on how to solve problems.
Django, as a Web framework provides a programming infrastructure for your applications, so that you can focus on writing clean, maintainable code without having to reinvent the wheel.
The MVC Design Pattern
Django follows a pattern called Model-View-Controller(MVC). Simply put, MVC is a way of developing software so that the code for defining and accessing data (the model) is separate from request-routing logic (the controller), which inturn is separate from the user interface (the view). While using Django framework, the entire code is split into four python files as models.py, views.py, urls.py and an HTML template file.
Django, as a Web framework provides a programming infrastructure for your applications, so that you can focus on writing clean, maintainable code without having to reinvent the wheel.
The MVC Design Pattern
Django follows a pattern called Model-View-Controller(MVC). Simply put, MVC is a way of developing software so that the code for defining and accessing data (the model) is separate from request-routing logic (the controller), which inturn is separate from the user interface (the view). While using Django framework, the entire code is split into four python files as models.py, views.py, urls.py and an HTML template file.
- The models.py file contains a description of the database table, represented by a python class. This class is called a model. Using it, you can create, retrieve, update and delete records in your database using simple python code rather than writing repetitive SQL statements.
- The views.py file contains the business logic for the page.
- The urls.py file specifies which view is called for a given URL pattern.
A key advantage of such an approach is that components are loosely coupled. Each distinct piece of a Django-powered Web application has a single key purpose and can be changed independently without affecting the other pieces. For example, a developer can change the URL for a given part of the application without affecting the underlying implementation. A designer can change a page's HTML without having to touch the python code that renders it. A database administrator can rename a database table and specify the change in a single place, rather than having to search and replace through a dozen of files.
Installing Django
Django itself is written purely in Python, so the first step in installing the framework is to make sure you have Python installed. Official releases have a version number, such as 1.0.3 or 1.1, and the latest one is always available at http://www.djangoproject.com/download/.
In the case of linux you have to download the tarball, unzip it and then run setup.py install. The steps to be followed are given below.
Testing the Django installation
- tar xzvf Django-1.0.2-final.tar.gz
- cd Django-*
- sudo python setup.py install
Testing the Django installation
To check whether Django is installed successfully, start the python interactive interpreter by typing python. If the installation was successful, you should be able to import the module django.
Setting up a Database
Django supports four database engines:
>>>import django
>>>django.VERSION
(1, 1, 0, 'final', 1)
Setting up a Database
Django supports four database engines:
- PostgreSQL (http://www.postgresql.org/)
- SQLite 3 (http://www.sqlite.org/)
- MySQL (http://www.mysql.com/)
- Oracle (http://www.oracle.com/)
- First, you’ll need to install and configure the database server itself. If you’re on a shared hosting provider, odds are that they’ve set this up for you already.
- Second, you’ll need to install the Python library for your particular database backend. This is a third-party bit of code that allows Python to interface with the database.
Starting a Project
Once you have installed python, Django and your Database server/library, you can step in developing a Django application by creating a project. A project is a ccollection of settings for an instance of Django, including database configuration. Django-specific options and application-specific settings. The startproject command creates a directory containing four files:
Once you have installed python, Django and your Database server/library, you can step in developing a Django application by creating a project. A project is a ccollection of settings for an instance of Django, including database configuration. Django-specific options and application-specific settings. The startproject command creates a directory containing four files:
mysite/
__init__.py
manage.py
settings.py
urls.py
These files are as follows:- __init__.py: A file required for python to treat the project directory as a package (i.e., a group of python modules). Its an empty file, and generally you wont add anything to it.
- manage.py: A command-line utility that lets you interact with this Django project in various ways. Type python manage.py help to get a feel for what it can do. You should never have to edit this file. Its created in this directory purely for convenience.
- settings.py: Settings for the Django project. Take a look at it to get an idea of the types of settings available, along with their default values.
- urls.py: The URL for the Django project. Think of this as the table of contents of your Django-powered site.
Running the Development Server
The Django development server is a built-in, lightweight Web server you can use while developing your site. IT's included with Django so that you can develop your site rapidly, without having to deal with configuring your production server until you are ready for production. The development server watches your code and automatically reloads it, making it easy for you to change your code without needing to restart anything. To start the server, change into your project directory, if you haven't already, and run this command.
python manage.py runserver
You will see something like this:
Validating models....
0 errors found.
Django version 1.0, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C
This launches the server locally, on port 8000, accessible only to connections from your own computer. Now that it's running, visit http://127.0.0.1:8000/ with your Web browser. You will see a "Welcome to Django" page shaded in a pleasent pastel blue which indicates thet it is working.
Comments
Post a Comment