from django.db import models
class Continent(models.Model):
name = models.CharField(max_length=60, default='', unique=True)
code = models.CharField(max_length=3, default='')
def __str__(self):
return '%s %s' % (self.name, self.code)
class Meta:
ordering = ["name"]
verbose_name_plural = "continents"
class Country(models.Model):
name = models.CharField(max_length=60, default='', unique=True)
code = models.CharField(max_length=3, default='', unique=True)
continent = models.ForeignKey(Continent, default='', related_name='countries')
area = models.PositiveIntegerField(default=0)
population = models.PositiveIntegerField(default=0)
capital = models.CharField(max_length=60, default='')
def __str__(self):
return '%s %s %s %s %s %s' % (self.name, self.code, self.capital, self.population, self.area, self.continent)
class Meta:
ordering = ["name"]
verbose_name_plural = "countries"
from django.http import Http404from django.http import HttpResponsefrom django.http import JsonResponseimport json
from .models import Continent, Country
def continent_json(request, continent_code): all_continents = Continent.objects.all() my_dictionary = {} for continent in all_continents: if continent.code == continent_code: for country in continent.countries.all(): my_dictionary[country.code] = country.name response = JsonResponse(my_dictionary) response.status_code = 200 callback = request.GET.get('callback') if not callback: return HttpResponse(response, content_type="application/json") response = '{0}({1})'.format(callback, response) return HttpResponse(response, content_type="application/javascript") raise Http404("Not implemented")
def country_json(request, continent_code, country_code): all_countries = Country.objects.all() for country in all_countries: if country.code == country_code: if country.continent.code == continent_code: area = country.area population = country.population capital = country.capital dictionary = dict([('area', area), ('population', population), ('capital', capital)]) obj = json.dumps(dictionary, indent=4) response = JsonResponse(dictionary) response.status_code = 200 callback = request.GET.get('callback') if not callback: return HttpResponse(response, content_type="application/json") response = '{0}({1})'.format(callback, response) return HttpResponse(response, content_type="application/javascript") raise Http404()
{% extends "base.html" %}
{% load staticfiles %}
{% block title %}Continents{% endblock %}
{% block header %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static 'selectui/style.css' %}">
<script type="text/javascript" src="{% static 'selectui/ajax.js' %}"></script>
{% endblock %}
{% block content %}
<p>If you see this, your application is correctly setup.</p>
{% include "selectui/continentmenu.html" %}
<h2 id="continentName">{{ continent.name }}</h2>
<div id="tableContainer">
{% if continent %}
{% include "selectui/countrytable.html" %}
{% endif %}
</div>
{% endblock %}
<table id="countryTable">
<thead>
<tr>
<th>Name</th>
<th>Capital</th>
<th>Population</th>
<th>Area</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sweden</td>
<td>Stockholm</td>
<td>9045000</td>
<td>449964 km2</td>
</tr>
</tbody>
</table>
<ul id="continentMenu">
<li><a href="insert-path-to-continent-using-the-template">Scandinavia</a></li>
</ul>
Hi, I'm trying to set up Templates for Django. I already have models and views that function, and now I'm trying to figure out a way to get an actual page to show for it.
<snip>
The views return JSON-formatted information, as you can see. What I'm trying to achieve in the template is implement templates that display countries and their data in a table on the bottom of a page. The table should only be visible after user requests this view with a country code, and above the table I'm trying to have links to all the continents in the database.