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 }}
<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 %}



Here's an example what countrytable with one country would display

<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>

and here's an example of continentmenu.html

<ul id="continentMenu">
  <li><a href="insert-path-to-continent-using-the-template">Scandinavia</a></li>
</ul>


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 and how I should actually start building the 
templates themselves. 


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3d93be3d-1bc4-44b1-a7df-10bff534c228%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to