Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added example project

  • Loading branch information...
commit 9f1b7baea2484fb73878df45705a6d880b4f07e5 1 parent 1b8a6fb
Ben Dowling authored
0  examples/__init__.py
No changes.
13 examples/manage.py
... ... @@ -0,0 +1,13 @@
  1 +#!/usr/bin/env python
  2 +import sys
  3 +sys.path.append('..')
  4 +from django.core.management import execute_manager
  5 +try:
  6 + import settings # Assumed to be in the same directory.
  7 +except ImportError:
  8 + import sys
  9 + sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
  10 + sys.exit(1)
  11 +
  12 +if __name__ == "__main__":
  13 + execute_manager(settings)
35 examples/settings.py
... ... @@ -0,0 +1,35 @@
  1 +import os
  2 +def rel(path):
  3 + return os.path.join(os.path.abspath(os.path.dirname(__file__)), path)
  4 +
  5 +DEBUG = True
  6 +TEMPLATE_DEBUG = DEBUG
  7 +
  8 +DATABASES = {
  9 + 'default': {
  10 + 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  11 + 'HOST': 'localhost',
  12 + 'NAME': 'cities',
  13 + 'USER': 'DB_USER',
  14 + 'PASSWORD': 'DB_PASS',
  15 + }
  16 +}
  17 +
  18 +TEMPLATE_DIRS = (rel("templates"))
  19 +TIME_ZONE = 'UTC'
  20 +LANGUAGE_CODE = 'en-us'
  21 +
  22 +SITE_ID = 1
  23 +
  24 +SECRET_KEY = 'YOUR_SECRET_KEY'
  25 +
  26 +MIDDLEWARE_CLASSES = (
  27 +)
  28 +
  29 +ROOT_URLCONF = 'examples.urls'
  30 +
  31 +INSTALLED_APPS = (
  32 + 'cities',
  33 +)
  34 +
  35 +CITIES_POSTAL_CODES = ['US','UK']
2  examples/templates/base.html
... ... @@ -0,0 +1,2 @@
  1 +{% block content %}
  2 +{% endblock %}
9 examples/templates/countries.html
... ... @@ -0,0 +1,9 @@
  1 +{% extends "base.html" %}
  2 +{% block content %}
  3 + <h2>Countries</h2>
  4 + <ul>
  5 + {% for country in object_list %}
  6 + <li><a href="/{{ country.slug }}">{{ country.name }}</a></li>
  7 + {% endfor %}
  8 + </ul>
  9 +{% endblock %}
18 examples/templates/list.html
... ... @@ -0,0 +1,18 @@
  1 +{% extends "base.html" %}
  2 +{% block content %}
  3 + {% if place %}
  4 + <a href="/">Places</a> &raquo;
  5 + {% endif %}
  6 + {% for object in place.hierarchy %}
  7 + {% if not forloop.last %}
  8 + <a href="/{{ object.get_absolute_url }}">{{ object.name }}</a> &raquo;
  9 + {% else %}
  10 + {{ object.name }}
  11 + {% endif %}
  12 + {% endfor %}
  13 + <ul>
  14 + {% for object in object_list %}
  15 + <li><a href="/{{ object.get_absolute_url }}">{{ object.name }}</a></li>
  16 + {% endfor %}
  17 + </ul>
  18 +{% endblock %}
36 examples/urls.py
... ... @@ -0,0 +1,36 @@
  1 +from django.conf.urls.defaults import *
  2 +from django.conf.urls import patterns
  3 +from django.views.generic import ListView
  4 +from cities.models import Country, Region, City, District
  5 +
  6 +class PlaceListView(ListView):
  7 + template_name = "list.html"
  8 +
  9 + def get_queryset(self):
  10 + if not self.args or not self.args[0]:
  11 + self.place = None
  12 + return Country.objects.all()
  13 + args = self.args[0].split("/")
  14 +
  15 + country = Country.objects.get(slug=args[0])
  16 + if len(args) == 1:
  17 + self.place = country
  18 + return Region.objects.filter(country=country).order_by('name')
  19 +
  20 + region = Region.objects.get(country = country, slug=args[1])
  21 + if len(args) == 2:
  22 + self.place = region
  23 + return City.objects.filter(region=region).order_by('name')
  24 +
  25 + city = City.objects.get(region = region, slug=args[2])
  26 + self.place = city
  27 + return District.objects.filter(city=city).order_by('name')
  28 +
  29 + def get_context_data(self, **kwargs):
  30 + context = super(PlaceListView, self).get_context_data(**kwargs)
  31 + context['place'] = self.place
  32 + return context
  33 +
  34 +urlpatterns = patterns('',
  35 + (r'^(.*)$', PlaceListView.as_view()),
  36 +)

0 comments on commit 9f1b7ba

Please sign in to comment.
Something went wrong with that request. Please try again.