Django - Basic

django

What is a project?

Django has the concept of a project, which is essentially a set of global files that dictate behavior and manage one or more applications. You can think of a project as a single website with one or more apps that run underneath it like a blog, guestbook, etc.

What is the basic structure of a project?

In Django, a project basically consists of 4 files (init.py, urls.py, settings.py, manage.py) plus one or more app directories. Here are brief descriptions of the files:

  • __init__.py — tells Python this is a package
  • urls.py — global URL config ("URLconf") file
  • settings.py — project-specific configuration
  • manage.py — command-line interface for apps

What is an app?

As mentioned above, a project will then contain one or more apps (one folder per app). Each of these subdirectories will have their own set of files (init.py, models.py, views.py, tests.py) as well as an optional app-level URLconf and a templates directory. The four most important files are:

  • __init__.py — same as above
  • models.py — data models
  • views.py — request handlers
  • tests.py — unit tests (optional but strongly-encouraged)

In addition, an app can also be "rebranded" as an app package, a reusable piece of functionality that can be dropped into any Django project. One example of such an app package would be a full-text-search app like nonrel-search.

How can we create a new project?

django-admin.py startproject PROJECT_NAME
cd PROJECT_NAME

How can we create a new app?

./manage.py startapp APP_NAME
cd APP_NAME
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License