Managing static files (e.g. images, JavaScript, CSS)

Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”. Django provides django.contrib.staticfiles to help you manage them. This page describes how you can serve these static files. Configuring static files Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS. In your settings file, define STATIC_URL, for example: STATIC_URL = '/static/' In your templates, either hardcode the url lik

Making queries

Once you’ve created your data models, Django automatically gives you a database-abstraction API that lets you create, retrieve, update and delete objects. This document explains how to use this API. Refer to the data model reference for full details of all the various model lookup options. Throughout this guide (and in the reference), we’ll refer to the following models, which comprise a Weblog application: from django.db import models class Blog(models.Model): name = models.CharField(max_

JavaScript customizations in the admin

Inline form events New in Django 1.9. You may want to execute some JavaScript when an inline form is added or removed in the admin change form. The formset:added and formset:removed jQuery events allow this. The event handler is passed three arguments: event is the jQuery event. $row is the newly added (or removed) row. formsetName is the formset the row belongs to. The event is fired using the django.jQuery namespace. In your custom change_form.html template, extend the admin_change_for

Introduction to class-based views

Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views: Organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching. Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components. The rel

Installing Geospatial libraries

GeoDjango uses and/or provides interfaces for the following open source geospatial libraries: Program Description Required Supported Versions GEOS Geometry Engine Open Source Yes 3.4, 3.3 PROJ.4 Cartographic Projections library Yes (PostgreSQL and SQLite only) 4.9, 4.8, 4.7, 4.6, 4.5, 4.4 GDAL Geospatial Data Abstraction Library Yes (SQLite only) 2.1, 2.0, 1.11, 1.10, 1.9, 1.8, 1.7 GeoIP IP-based geolocation library No 1.4 PostGIS Spatial extensions for PostgreSQL Yes (PostgreSQL only) 2.3, 2.2

http.StreamingHttpResponse.streaming_content

StreamingHttpResponse.streaming_content An iterator of strings representing the content.

Installing PostGIS

PostGIS adds geographic object support to PostgreSQL, turning it into a spatial database. GEOS, PROJ.4 and GDAL should be installed prior to building PostGIS. You might also need additional libraries, see PostGIS requirements. The psycopg2 module is required for use as the database adapter when using GeoDjango with PostGIS. On Debian/Ubuntu, you are advised to install the following packages: postgresql-x.x, postgresql-x.x-postgis, postgresql-server-dev-x.x, python-psycopg2 (x.x matching the Pos

Internationalization and localization

Overview The goal of internationalization and localization is to allow a single Web application to offer its content in languages and formats tailored to the audience. Django has full support for translation of text, formatting of dates, times and numbers, and time zones. Essentially, Django does two things: It allows developers and template authors to specify which parts of their apps should be translated or formatted for local languages and cultures. It uses these hooks to localize Web apps f

Installing SpatiaLite

SpatiaLite adds spatial support to SQLite, turning it into a full-featured spatial database. First, check if you can install SpatiaLite from system packages or binaries. For example, on Debian-based distributions, try to install the spatialite-bin package. For distributions that package SpatiaLite 4.2+, install libsqlite3-mod-spatialite. For Mac OS X, follow the instructions below. For Windows, you may find binaries on the Gaia-SINS home page. In any case, you should always be able to install f

Integrating Django with a legacy database

While Django is best suited for developing new applications, it’s quite possible to integrate it into legacy databases. Django includes a couple of utilities to automate as much of this process as possible. This document assumes you know the Django basics, as covered in the tutorial. Once you’ve got Django set up, you’ll follow this general process to integrate with an existing database. Give Django your database parameters You’ll need to tell Django what your database connection parameters are