Friday, January 3, 2014

Creating a project

You can download Django from the official website or search for OS specific downloads on the internet.
Let's create a sample college website complete with user login and a student database.

To create the project:

django-admin startproject mysite

This command will start a project called 'mysite'. If you open the folder labeled 'mysite'  you'll find the following things:
  • __init__.py: This is the initialization file. There is normally nothing in this file, and your app will work very well if you keep a cool, wary distance from this file.
  • manage.py: This file issues commands for running the development server, setting up and synchronizing the database etc. Again, this file is not to be altered.
  • settings.py: This file is going to be altered a lot by us. This contains all the settings for database selection, application listings, middleware classes etc.
  • urls.py: Another file that will be altered heavily. This file contains all the URLs that our website will contain and their specific webpages.


Adding applications:
The part up till now was only the project skeleton. We need to add apps to our website to make it more useful. Technically, you can have the entire website up and  running with a single app, but that's messy and a bad practice. Here, 'app' doesn't meant much the same thing as everyone knows. As we progress with the tutorial, the meaning will get clearer.

First, change into the project directory.
cd mysite

Then, add your app.
django-admin startapp website

Now, if you enter the folder labeled 'website', this is the list of files that you will see.

  • __init__.py: Again, initialization file and not to be tampered with.
  • models.py: In this file you will be defining the tables in sql that you will be using in the form of Python classes. Django knows how to translate these classes into sql tables in the appropriate form.
  • tests.py: This file is used for designing tests for the code. Many times, to check if a particular webpage is working, you have to navigate through a labyrinth of other pages to get to the desired page and look for bugs. This file automates this process, thus reducing developer effort.
  • views.py: This file is responsible for the user interface on the webpages. Here we will be using CSS, JavaScript and other languages to make our webpages more attractive.


That's all for now. We will start implementing our website in the next part of the tutorial.

No comments:

Post a Comment