【发布时间】:2015-10-31 07:04:27
【问题描述】:
我是 django 的新手。我正在按照 tangowithdjango.com 中给出的教程进行操作。我不知道为什么会出现错误............ 错误是
Exception Type: FieldError
Exception Value: Cannot resolve keyword 'likes' into field. Choices are: id, name, page
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py in names_to_path, line 1397
Python Executable: /usr/bin/python
Python Version: 2.7.9
Python Path:
['/root/workspace/tango_with_django',
'/usr/local/lib/python2.7/dist-packages/virtualenv-13.1.2-py2.7.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
我的 populate_rango.py 文件是
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django.settings')
import django
django.setup()
from rango.models import Category, Page
def populate():
python_cat = add_cat('Python')
add_page(cat=python_cat,
title="Official Python Tutorial",
url="http://docs.python.org/2/tutorial/"
views=128,
likes=64
)
add_page(cat=python_cat,
title="How to Think like a Computer Scientist",
url="http://www.greenteapress.com/thinkpython/",
views=128,
likes=64)
add_page(cat=python_cat,
title="Learn Python in 10 Minutes",
url="http://www.korokithakis.net/tutorials/python/",
views=128,
likes=64)
django_cat = add_cat("Django")
add_page(cat=django_cat,
title="Official Django Tutorial",
url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/",
views=64,
likes=32)
add_page(cat=django_cat,
title="Django Rocks",
url="http://www.djangorocks.com/",
views=64,
likes=32)
add_page(cat=django_cat,
title="How to Tango with Django",
url="http://www.tangowithdjango.com/",
views=64,
likes=32)
frame_cat = add_cat("Other Frameworks")
add_page(cat=frame_cat,
title="Bottle",
url="http://bottlepy.org/docs/dev/",
views=32,
likes=16)
add_page(cat=frame_cat,
title="Flask",
url="http://flask.pocoo.org",
views=32,
likes=16)
for c in Category.objects.all():
for p in Page.objects.filter(category=c):
print "- {0} - {1}".format(str(c), str(p))
def add_page(cat, title, url, views=0,likes=0):
p = Page.objects.get_or_create(category=cat, title=title)[0]
p.url=url
p.views=views
p.likes=likes
p.save()
return p
def add_cat(name,views=0,likes=0):
c = Category.objects.get_or_create(name=name)[0]
c.views=views
c.likes=likes
c.save()
return c
if __name__ == '__main__':
print "Starting Rango population script..."
populate()
和 admin.py 文件是
from django.contrib import admin
from rango.models import Category,Page
from rango.models import views,likes
# Register your models here.
class PageAdmin(admin.ModelAdmin):
list_display=('title','category','url')
admin.site.register(Category)
admin.site.register(views)
admin.site.register(likes)
admin.site.register(Page,PageAdmin)
models.py 是
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
def __unicode__(self): #For Python 2, use __str__ on Python 3
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
def __unicode__(self):
return self.title
class views(models.Model):
number=models.IntegerField()
def __unicode__(self):
return self.number
class likes(models.Model):
num=models.IntegerField()
def __unicode__(self):
return self.num
请帮助我解决错误。感谢您花费一些时间...
【问题讨论】:
-
你有完整的回溯吗?
-
是的。你要我上传吗?
-
完整回溯是什么意思?我是 django 新手。所以,我不知道它到底是什么。@IgnacioVazquez-Abrams
-
你能展示你的模型吗?
-
我已经上传了models.py @AlexLisovoy