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.
My models are
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"
and my views are
from django.http import Http404
from django.http import HttpResponse
from django.http import JsonResponse
import 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()
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.
In this exercise, you need to implement templates that will display
countries and their data in a table on the bottom of this page. The table
should only be visible when a user has requested this view with a country
code. Above the table there should be links to all continents in the
database. If the user requests a page with a non existent continent code a
404 HTTP response code should be returned.
I already have templates files for what I need to create, but I'm not sure
how I can actually get to it. I have 2 templates I need to create:
countrytable.html and continentmenu.html, and they're an extension of
index.html.
index.html:
{% extends "base.html" %}
{% load staticfiles %}
{% block title %}Continents{% endblock %}
{% block header %}
{{ block.super }}
{% endblock %}
{% block content %}
If you see this, your application is correctly setup.
{% include "selectui/continentmenu.html" %}
{{ continent.name }}
{% if continent %}
{% include "selectui/countrytable.html" %}
{% endif %}
{% endblock %}
Here's an example what countrytable with one country would display
Name
Capital
Population
Area
Sweden
Stockholm
9045000
449964 km2
and here's an example of continentmenu.html
Scandinavia
I'm a bit lost as to how the hierarchy between the data works in Django, so
I'm not sure if I need to figure out a way to include the data from my
models in the templates