2014-09-27 38 views
0

我目前正在研究以下教程http://www.tangowithdjango.com/book/chapters/models_templates.htmlTangoWithDjango | 6.2.3修改索引模板|无法获取类别列表

我在生成Rango的主页时失败,但出现以下错误。我对以前的所有章节都做了正确的描述。

我使用Ubuntu 14.04和Django 1.7

我从生成兰戈网页有错误是如下:

模板时发生错误呈现

在模板中的/ home /威廉/ tango_with_django_project/templates/rango/index.html,第10行错误 无法将关键字'likes'解析为字段。选项包括:ID,姓名,

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这里是文件和目录的位置/首页/威廉页/ 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 

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

回答

0

见第5.10第一颗子弹......在你的分类模型的喜欢字段不存在,但你所要求的命令在你看来这个领域。

创建像模型和迁移,你应该建立和运行:)

+0

您的意见非常感谢后,我得到了固定部分5.10整理运动后它 – 2014-09-28 03:28:52