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>
您的意見非常感謝後,我得到了固定部分5.10整理運動後它 – 2014-09-28 03:28:52