就像页面 Headers 所说的那样,按照链接教程,我就是Getting Started with Tastypie . 但是当我尝试加载 /api/entry/?format=json
时,我收到HTTP 500响应,并显示以下错误消息:
no such table: myapp_entry
当我查看sqlite3时,确实没有这样的表 .
这是我遵循教程的方式:
$ django-admin startproject mysite
$ cd mysite
$ django-admin startapp myapp
我按照教程中的指定创建/编辑了 myapp/models.py
, myapp/api.py
和 mysite/urls.py
,并在 mysite/settings.py
中将'tastypie'添加到我的 INSTALLED_APPS
中 .
Note: 我不清楚要编辑或创建哪个 urls.py
文件,所以我编辑了 mysite
中的现有文件 . 所以现在它看起来像这样:
from django.conf.urls import url, include
from myapp.api import EntryResource
from django.contrib import admin
entry_resource = EntryResource()
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^blog/', include('myapp.urls')),
url(r'^api/', include(entry_resource.urls)),
]
我注释掉了'blog'行,因为它导致错误 ImportError: No module named 'myapp.urls' . 我认为这是我填充的步骤,但是当我尝试将教程代码放在 myapp/urls.py
中时,我在尝试加载页面时得到了404,当我尝试将 url(r'^blog/', include('myapp.urls'))
添加到 mysite/urls.py
时,我得到了堆栈溢出 . 所以我回到了上面显示的代码 .
要清楚,这就是我的文件结构现在的样子:
$ find . -type f -not -name '*.pyc'
./manage.py
./myapp/__init__.py
./myapp/views.py
./myapp/models.py
./myapp/tests.py
./myapp/admin.py
./myapp/apps.py
./myapp/migrations/__init__.py
./myapp/api.py
./db.sqlite3
./mysite/__init__.py
./mysite/settings.py
./mysite/urls.py
./mysite/wsgi.py
我做的另一个更改是将 Meta
子类添加到我的 Entry
类中,所以前十几行看起来像这样:
class Entry(models.Model):
user = models.ForeignKey(User)
pub_date = models.DateTimeField(default=now)
title = models.CharField(max_length=200)
slug = models.SlugField(null=True, blank=True)
body = models.TextField()
class Meta:
app_label = 'myapp'
# __unicode__() and save() as in the tutorial
如果我不这样做,我在控制台中得到这个: RuntimeError: Model class myapp.models.Entry doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
我尝试的另一件事是 python manage.py migrate
. 它找到了要做的事情,但这并没有解决这个错误 .
这里安装了's what I'(在 requirements.txt
/ virtualenv
中):
-
Django(1.10)
-
django-tastypie(0.13.3)
-
PyYAML(3.12)
-
mysqlclient(1.3.10)(虽然我现在没有使用默认的sqlite3设置't actually set it up yet—I'm)
我正在运行Python 3.4.3,但是在Python 2.7.6中使用非常类似的设置我得到了完全相同的错误 .
最后,这是来自HTTP 500页面的完整堆栈跟踪:
{"error_message": "no such table: myapp_entry", "traceback": "Traceback (most recent call last):
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: myapp_entry
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 219, in wrapper
response = callback(request, *args, **kwargs)
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 450, in dispatch_list
return self.dispatch('list', request, **kwargs)
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 482, in dispatch
response = method(request, **kwargs)
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/resources.py", line 1335, in get_list
to_be_serialized = paginator.page()
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/paginator.py", line 194, in page
count = self.get_count()
File "~/myproject/virtualenv/lib/python3.4/site-packages/tastypie/paginator.py", line 126, in get_count
return self.objects.count()
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 369, in count
return self.query.get_count(using=self.db)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/query.py", line 476, in get_count
number = obj.get_aggregation(using, ['__count'])['__count']
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/query.py", line 457, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "~/myproject/virtualenv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: myapp_entry
"}
谁能看到我做错了什么?或者本教程不适用于这些版本?
1 回答
sqlite3.OperationalError: no such table: myapp_entry
首先,你的项目是使用sqlite,而不是mysql . 您应该更新
DATABASES
设置 .在项目设置中将
myapp
添加到INSTALLED_APPS
.然后,运行
./manage.py migrate
. 在修复INSTALLED_APPS
之后,Django应该能够找到应用程序的Entry
模型并为其创建表格 .