我正在使用Django教程处理Tango,在exercises for the fifth section中,我偶然发现了。我在这个主题上使用了另一个StackOverflow问题的解决方案,但我一直收到这个错误:
错误:没有这样的列:rango_category.views
这是我的代码:
populate_rango.py
import os
def populate():
python_cat = add_cat('Python', views=128, likes=64)
add_page(cat=python_cat,
title="Official Python Tutorial",
url="http://docs.python.org/2/tutorial/")
add_page(cat=python_cat,
title="How to Think like a Computer Scientist",
url="http://www.greenteapress.com/thinkpython/")
add_page(cat=python_cat,
title="Learn Python in 10 Minutes",
url="http://www.korokithakis.net/tutorials/python/")
django_cat = add_cat("Django", views=64, likes=32)
add_page(cat=django_cat,
title="Official Django Tutorial",
url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/")
add_page(cat=django_cat,
title="Django Rocks",
url="http://www.djangorocks.com/")
add_page(cat=django_cat,
title="How to Tango with Django",
url="http://www.tangowithdjango.com/")
frame_cat = add_cat("Other Frameworks", views=32, likes=16)
add_page(cat=frame_cat,
title="Bottle",
url="http://bottlepy.org/docs/dev/")
add_page(cat=frame_cat,
title="Flask",
url="http://flask.pocoo.org")
# Print out what we have added to the user.
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):
p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]
def add_cat(name, views=0, likes=0):
c = Category.objects.get_or_create(name=name, views=views, likes=likes)[0]
return c
# Start execution here!
if __name__ == '__main__':
print "Starting Rango population script..."
os.environ.setdefault('DJANGO_SETTINGS_MODULE','tango_with_django_project.settings')
from rango.models import Category, Page
populate()
models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0, unique=False)
likes = models.IntegerField(default=0, unique=False)
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, unique=False)
def __unicode__(self):
return self.title
admin.py
from django.contrib import admin
from rango.models import Category, Page
class PageAdmin(admin.ModelAdmin):
list_display = ('title', 'category', 'url')
class CatAdmin(admin.ModelAdmin):
list_display = ('views', 'likes')
admin.site.register(Category, CatAdmin)
admin.site.register(Page, PageAdmin)
希望你们能帮忙!
答案 0 :(得分:1)
您似乎收到此错误,因为表或列实际上并未在您的数据库中创建。如果您已经运行django-admin.py syncdb
,则可能需要查看创建的实际结构。
答案 1 :(得分:1)
有两种可能性来解决此错误 1)在执行syncdb之后,您为模型添加了额外的字段。 2)你在django中为model.py文件添加了新类。
解决方法是:
首先使用命令
向南安装 for windows: **easy_install south** //for that you need to go to the script folder of python folder in c drive.
for linux: **sudo easy_install south**
然后按照此处migration tutorials
中包含的步骤操作step1- python manage.py schemamigration your_app_name --initial
step-2 python manage.py migrate your_app_name
如果您对模型进行了任何更改,那么
1) python manage.py schemamigration your_app_name --auto
2) python manage.py migrate your_app_name
希望这会对你有所帮助。
答案 2 :(得分:0)
答案 3 :(得分:0)
我遇到了同样的问题,在我改成 str 而不是unicode后,它可以工作了。 (也许您像我一样,使用Python 3)