TangoWithDjango | 6.2.3修改索引模板|无法获取类别列表

时间:2014-09-27 05:38:31

标签: python django

我目前正在处理以下教程http://www.tangowithdjango.com/book/chapters/models_templates.html

我没有成功生成Rango的主页,但有以下错误。我之前的所有章节都是正确的。

我正在使用Ubuntu 14.04和Django 1.7

我从生成rango网页时得到的错误如下:

模板渲染期间出错

在模板/home/william/tango_with_django_project/templates/rango/index.html中,第10行的错误 无法解析关键字'喜欢'进入田野。选择是:id,name,page

1   <!DOCTYPE html>
2   <html>
3   <head>
4   <title>Rango</title>
5   </head>
6   
7   <body>
8   <h1>Rango says...hello world!</h1>
9   
10  {% if categories %}
11  <ul>
12  {% for category in categories %}
13  <li>{{ category.name }}</li>
14  {% endfor %}
15  </ul>
16  {% else %}
17  <strong>There are no categories present.</strong>
18  {% endif %}
19  
20  <a href="/rango/about/">About</a>

我正在研究Ubuntu 14.04 LTS这里是文件和目录位置/ home / william / tango_with_django_project

- manage.py
- populate_rango.py
- tango_with_django_project [directory]
---- settings.py
---- urls.py
- rango [directory]
---- views
---- models.py
- templates [directory]
---- rango [directory]
-------- index.html

models.py

from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    def __unicode__(self):
        return self.name

class Page(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=128)
    url = models.URLField()
    views = models.IntegerField(default=0)

    def __unicode__(self):
        return self.title

兰戈/ viewspy

from django.template import RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponse
from rango.models import Category

def index(request):
    # Obtain the context from the HTTP request.
    context = RequestContext(request)

    # Query the database for a list of ALL categories currently stored.
    # Order the categories by no. likes in descending order.
    # Retrieve the top 5 only - or all if less than 5.
    # Place the list in our context_dict dictionary which will be passed to the template engine.
    category_list = Category.objects.order_by('-likes')[:5]
    context_dict = {'categories': category_list}

    # Render the response and send it back!
    return render_to_response('rango/index.html', context_dict, context)

模板/兰戈/ index.html中

<!DOCTYPE html>
<html>
    <head>
        <title>Rango</title>
    </head>

    <body>
        <h1>Rango says...hello world!</h1>

        {% if categories %}
            <ul>
                {% for category in categories %}
                <li>{{ category.name }}</li>
                {% endfor %}
            </ul>
        {% else %}
            <strong>There are no categories present.</strong>
        {% endif %}

        <a href="/rango/about/">About</a>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

请参阅第5.10节下的第一个项目符号...类别模型中的likes字段不存在,但您要求在视图中按该字段排序。

创建Like模型并进行迁移后,您应该启动并运行:)