background preloader

Django

Facebook Twitter

Quick install guide. Before you can use Django, you’ll need to get it installed. We have a complete installation guide that covers all the possibilities; this guide will guide you to a simple, minimal installation that’ll work while you walk through the introduction. Install Python Being a Python Web framework, Django requires Python. It works with Python 2.7, 3.2, 3.3, or 3.4. All these versions of Python include a lightweight database called SQLite so you won’t need to set up a database just yet. Get the latest version of Python at or with your operating system’s package manager. Django on Jython If you use Jython (a Python implementation for the Java platform), you’ll need to follow a few additional steps.

You can verify that Python is installed by typing python from your shell; you should see something like: Python 3.3.3 (default, Nov 26 2013, 13:33:18) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information Set up a database Install Django. Django settings. A Django settings file contains all the configuration of your Django installation. This document explains how settings work and which settings are available. The basics A settings file is just a Python module with module-level variables. Here are a couple of example settings: DEBUG = FalseDEFAULT_FROM_EMAIL = 'webmaster@example.com'TEMPLATE_DIRS = ('/home/templates/mike', '/home/templates/john') Because a settings file is a Python module, the following apply: It doesn’t allow for Python syntax errors.It can assign settings dynamically using normal Python syntax.

Designating the settings When you use Django, you have to tell it which settings you’re using. The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. The django-admin utility When using django-admin, you can either set the environment variable once, or explicitly pass in the settings module each time you run the utility. Example (Unix Bash shell): Example (Windows shell): On the server (mod_wsgi)

URL dispatcher. A clean, elegant URL scheme is an important detail in a high-quality Web application. Django lets you design URLs however you want, with no framework limitations. There’s no .php or .cgi required, and certainly none of that 0,2097,1-1-1928,00 nonsense. See Cool URIs don’t change, by World Wide Web creator Tim Berners-Lee, for excellent arguments on why URLs should be clean and usable. Overview To design URLs for an app, you create a Python module informally called a URLconf (URL configuration). This mapping can be as short or as long as needed. Django also provides a way to translate URLs according to the active language. How Django processes a request When a user requests a page from your Django-powered site, this is the algorithm the system follows to determine which Python code to execute: Django determines the root URLconf module to use.

Example Here’s a sample URLconf: Notes: Example requests: A request to /articles/2005/03/ would match the third entry in the list. Named groups Performance. Django-admin.py and manage.py. Django-admin is Django’s command-line utility for administrative tasks. This document outlines all it can do. In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin that takes care of several things for you before delegating to django-admin: The django-admin script should be on your system path if you installed Django via its setup.py utility.

If it’s not on your path, you can find it in site-packages/django/bin within your Python installation. Consider symlinking it from some place on your path, such as /usr/local/bin. For Windows users, who do not have symlinking functionality available, you can copy django-admin.exe to a location on your existing path or edit the PATH settings (under Settings - Control Panel - System - Advanced -Environment...) to point to its installed location. Generally, when working on a single Django project, it’s easier to use manage.py than django-admin. Check <appname appname django-admin check. How to install Django. This document will get you up and running with Django.

Install Python Being a Python Web framework, Django requires Python. It works with Python 2.7, 3.2 or 3.3. Get the latest version of Python at or with your operating system’s package manager. Django on Jython If you use Jython (a Python implementation for the Java platform), you’ll need to follow a few additional steps. Install Apache and mod_wsgi If you just want to experiment with Django, skip ahead to the next section; Django includes a lightweight web server you can use for testing, so you won’t need to set up Apache until you’re ready to deploy Django in production.

If you want to use Django on a production site, use Apache with mod_wsgi. mod_wsgi can operate in one of two modes: an embedded mode and a daemon mode. See How to use Django with mod_wsgi for information on how to configure mod_wsgi once you have it installed. Get your database running Remove any old versions of Django Install the Django code.