我刚开始学习如何使用django。我在CLI中出现了这个错误
File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py", line 1337,
in setup_
"Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'likes' into field. Choices are: id, name,
page
我的问题是我不知道它意味着什么或如何解决它。有人可以解释一下吗? 这是调试器日志给我的输出:
Error during template rendering
In template C:\Users\aharon\Desktop\TEMP\index.html, error at line 40
Cannot resolve keyword 'likes' into field. Choices are: id, name, page
30 </html>
31
32 <html>
33 <head>
34 <title>Rango</title>
35 </head>
36
37 <body>
38 <h1>Rango says...hello world!</h1>
39
40 {% if categories %} <--this was highlighted in the debugger
41 <ul>
42 {% for category in categories %}
43 <li>{{ category.name }}</li>
44 {% endfor %}
45 </ul>
46 {% else %}
47 <strong>There are no categories present.</strong>
48 {% endif %}
49
50 <a href="/rango/about/">About</a>
查看代码:
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from rango.models import Category
def index(request):
context = RequestContext(request)
category_list = Category.objects.order_by('-likes')[:5]
context_dict = {'categories': category_list}
return render_to_response('index.html', context_dict, context)
答案 0 :(得分:2)
问题是由以下行引起的:
category_list = Category.objects.order_by('-likes')[:5]
Category
模型似乎没有字段likes
,但id
,name
和page
。
答案 1 :(得分:1)
您的问题在模型LIKES字段中导致
“喜欢”字段不在您的模型中。添加“赞”字段和运行migration。
答案 2 :(得分:0)
我不确定是否有更好的方法,但我在models.py中添加了喜欢这样看起来像这样:
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
likes = models.IntegerField(default=0) # I added this line
def __unicode__(self):
return self.name
接下来我删除了数据库,因为迁移因某种原因无效
然后: python manage.py syncdb
这修复了喜欢的问题,但它也杀死了我们制作populate_rango.py文件的教程前面步骤中的所有信息。所以我再次跑了。
python populate_rango.py
然后我的rango页面显示正确。
答案 3 :(得分:0)
在类别
中将此行添加到 django / models.py ....likes = models.IntegerField(default=0)
然后运行:
$ python manage.py makemmigrations
$ python manage.py migrate..
并刷新浏览器