Use the django-admin.py script like normal to create a geodjango project:
$ django-admin.py startproject geodjango
With the project initialized, now create a world Django application within the geodjango project:
$ cd geodjango $ python manage.py startapp world
The geodjango project settings are stored in the geodjango/settings.py file. Edit the database connection settings appropriately:
DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': 'geodjango', 'USER': 'geo', } }
In addition, modify the INSTALLED_APPS setting to include django.contrib.admin, django.contrib.gis, and world (our newly created application):
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'django.contrib.gis', 'world' )
'ENGINE': 'django.contrib.gis.db.backends.postgis',
Note
MySQL and Oracle users can skip this section because spatial types are already built into the database.
First, a spatial database needs to be created for our project. If using PostgreSQL and PostGIS, then the following commands will create the database from a spatial database template:
$ createdb -T template_postgis geodjango
$ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-2.0 # Creating the template spatial database. $ createdb -E UTF8 template_postgis $ createlang -d template_postgis plpgsql # Adding PLPGSQL language support. # Allows non-superusers the ability to create from this template $ psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';" # Loading the PostGIS SQL routines $ psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql $ psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql # Enabling users to alter spatial tables. $ psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;" $ psql -d template_postgis -c "GRANT ALL ON geography_columns TO PUBLIC;" $ psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
hese commands may be placed in a shell script for later use; for convenience the following scripts are available:
PostGIS version | Bash shell script |
---|---|
1.3 | create_template_postgis-1.3.sh |
1.4 | create_template_postgis-1.4.sh |
1.5 | create_template_postgis-1.5.sh |
Debian/Ubuntu | create_template_postgis-debian.sh |
Afterwards, you may create a spatial database by simply specifying template_postgis as the template to use (via the -T option):
$ createdb -T template_postgis <db name>