Django Introduction

Introduction

Django is a Web framework. Using Django, you can build and maintain high-quality Web applications with minimal fuss.

The MVC Design Pattern

MVC is way of developing software so that the code for defining and accessing data (the model) is separate from request-routing logic (the controller), which in turn is separate from the user interface (the view).

Django Projects

Definition

A project is a collection of settings for an instance of Django — including database configuration, Django-specific options and application-specific settings.

Creating a project

From the command line, cd into a directory where you’d like to store your code, then run the command django-admin.py startproject mysite. This will create a mysite directory in your current directory.

mysite/
    __init__.py
    manage.py
    settings.py
    urls.py

These files are:

  • init.py: An empty file that tells Python that this directory should be considered a python package
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. Available subcommands are in Django docs.
  • settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • urls.py: The URL declarations for this Django project.

Django Apps

Definition

  • An app is a collection of data and code that makes up some sort of logical unit.
  • Projects are composed of a number of apps.
  • Apps can be reused in multiple projects.

Creating an app

  • Start an new app: python manage.py startapp
  • Define models and views
  • Run syncdb when you add new models and they’ll be installed into the database. python manage.py syncdb

First Steps

  • Running the Development Server: Change into your project directory and run python manage.py runserver. This launches the server locally, on port 8000, accessible only to connections from your own computer. Visit http://127.0.0.1:8000/

Views and URLconfs

  • The contents of the page are produced by a view function, and the URL is specified in a URLconf.
  • View: a view is just a Python function that takes an HttpRequest as its first parameter and returns an instance of HttpResponse.
  • URLconfs: To hook a view function to a particular URL with Django, use a URLconf. It’s a mapping between URLs and the view functions that should be called for those URLs.

How Django Processes a Request

  • It all starts with the settings file. When you run python manage.py runserver, the script looks for a file called settings.py in the same directory as manage.py.
  • When a request comes in for a particular URL Django loads the URLconf pointed to by the ROOT_URLCONF setting.
  • Then it checks each of the URLpatterns in that URLconf, in order, comparing the requested URL with the patterns one at a time, until it finds one that matches.
  • When it finds one that matches, it calls the view function associated with that pattern, passing it an HttpRequest object as the first parameter.

settings.py

Basics

  • Normal Python module with variables
  • A settings file is a Python module with module-level variables
  • It doesn't allow for Python syntax errors.
  • It can assign settings dynamically.
  • It can import values from other settings files.

Designating the settings

  • To tell Djano which settings you are usign you have to use an environment variable DJANGO_SETTINGS_MODULE.
  • When using django-admin.py, you can either set the environment variable once, or explicitly pass in the settings module each time you run the utility.

Default settings

  • Each setting has a sensible default value. These defaults live in the module django/conf/global_settings.py.
  • The command python manage.py diffsettings displays differences between the current settings file and Django's default settings

Available Settings

All settings values are located in the Django documentation

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.