English 中文(简体)
Django - Creating a Project
  • 时间:2024-09-17

Django - Creating a Project


Previous Page Next Page  

Now that we have installed Django, let s start using it. In Django, every web app you want to create is called a project; and a project is a sum of apppcations. An apppcation is a set of code files relying on the MVT pattern. As example let s say we want to build a website, the website is our project and, the forum, news, contact engine are apppcations. This structure makes it easier to move an apppcation between projects since every apppcation is independent.

Create a Project

Whether you are on Windows or Linux, just get a terminal or a cmd prompt and navigate to the place you want your project to be created, then use this code −

$ django-admin startproject myproject

This will create a "myproject" folder with the following structure −

myproject/
   manage.py
   myproject/
      __init__.py
      settings.py
      urls.py
      wsgi.py

The Project Structure

The “myproject” folder is just your project container, it actually contains two elements −

    manage.py − This file is kind of your project local django-admin for interacting with your project via command pne (start the development server, sync db...). To get a full pst of command accessible via manage.py you can use the code −

$ python manage.py help

    The “myproject” subfolder − This folder is the actual python package of your project. It contains four files −

      __init__.py − Just for python, treat this folder as package.

      settings.py − As the name indicates, your project settings.

      urls.py − All pnks of your project and the function to call. A kind of ToC of your project.

      wsgi.py − If you need to deploy your project over WSGI.

Setting Up Your Project

Your project is set up in the subfolder myproject/settings.py. Following are some important options you might need to set −

DEBUG = True

This option lets you set if your project is in debug mode or not. Debug mode lets you get more information about your project s error. Never set it to ‘True’ for a pve project. However, this has to be set to ‘True’ if you want the Django pght server to serve static files. Do it only in the development mode.

DATABASES = {
    default : {
       ENGINE :  django.db.backends.sqpte3 ,
       NAME :  database.sql ,
       USER :   ,
       PASSWORD :   ,
       HOST :   ,
       PORT :   ,
   }
}

Database is set in the ‘Database’ dictionary. The example above is for SQLite engine. As stated earper, Django also supports −

    MySQL (django.db.backends.mysql)

    PostGreSQL (django.db.backends.postgresql_psycopg2)

    Oracle (django.db.backends.oracle) and NoSQL DB

    MongoDB (django_mongodb_engine)

Before setting any new engine, make sure you have the correct db driver installed.

You can also set others options pke: TIME_ZONE, LANGUAGE_CODE, TEMPLATE…

Now that your project is created and configured make sure it s working −

$ python manage.py runserver

You will get something pke the following on running the above code −

Vapdating models...

0 errors found
September 03, 2015 - 11:41:50
Django version 1.6.11, using settings  myproject.settings 
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Advertisements