尝试导入 TastyPie API 但我收到页面未找到消息

Trying to Import TastyPie API but I get Page Not Found Message

我正在尝试创建一个 Django 网站,更重要的是,导入 TastyPie API。但是每次我在本地主机上 运行 /articles/api/article 时,都会收到此错误消息:

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/articles/api/article

Using the URLconf defined in django_test.urls, Django tried these URL patterns, in this order:

    1. ^admin/
    2. ^accounts/login/$
    3. ^accounts/auth/$
    4. ^accounts/loggedin/$
    5. ^accounts/invalid/$
    6. ^accounts/logout/$
    7. ^accounts/register/$
    8. ^accounts/register_success/$
    9. ^articles/all/$
    10. ^articles/create/$
    11. ^articles/get/(?P<article_id>\d+)/$
    12. ^articles/like/(?P<article_id>\d+)/$
    13. ^articles/add_comment/(?P<article_id>\d+)/$
    14. ^articles/search/
    15. ^articles/api/article ^(?P<resource_name>article)/$ [name='api_dispatch_list']
    16. ^articles/api/article ^(?P<resource_name>article)/schema/$ [name='api_get_schema']
    17. ^articles/api/article ^(?P<resource_name>article)/set/(?P<pk_list>.*?)/$ [name='api_get_multiple']
    18. ^articles/api/article ^(?P<resource_name>article)/(?P<pk>.*?)/$ [name='api_dispatch_detail']

The current URL, articles/api/article, didn't match any of these.

这是我的 urls.py 文件,它位于我的 django_test/django_test 目录下:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django_test.api import ArticleResource

article_resource = ArticleResource()

urlpatterns = patterns('',
                       url(r'^admin/', include(admin.site.urls)),
                       url(r'^accounts/login/$', 'django_test.views.login'),
                       url(r'^accounts/auth/$', 'django_test.views.auth_view'),
                       url(r'^accounts/loggedin/$', 'django_test.views.loggedin'),
                       url(r'^accounts/invalid/$', 'django_test.views.invalid_login'),
                       url(r'^accounts/logout/$', 'django_test.views.logout'),
                       url(r'^accounts/register/$', 'django_test.views.register_user'),
                       url(r'^accounts/register_success/$', 'django_test.views.register_success'),
                       url(r'^articles/all/$', 'article.views.articles'),
                       url(r'^articles/create/$', 'article.views.create'),
                       url(r'^articles/get/(?P<article_id>\d+)/$', 'article.views.article'),
                       url(r'^articles/like/(?P<article_id>\d+)/$', 'article.views.like_article'),
                       url(r'^articles/add_comment/(?P<article_id>\d+)/$', 'article.views.add_comment'),
                       url(r'^articles/search/', 'article.views.search_titles'),
                       url(r'^articles/api/article', include(article_resource.urls)),

)

这是我的 api.py 文件,它也位于我的 django_test/django_test 目录中:

from tastypie.resources import ModelResource
from tastypie.constants import ALL
from article.models import Article 

class ArticleResource(ModelResource):
    class Meta:
        queryset = Article.objects.all()
        resource_name = 'article'

我想收到一条不同的错误消息:

"Sorry, not implemented yet. Please append "?format=json" 到你的 URL."

我真的很困惑,如果你能给我任何帮助,我将不胜感激。谢谢。

你在 urls.py 中的最后一行应该是:

url(r'^articles/api/', include(article_resource.urls)),

article部分由tastypie使用模型名称自动生成。你不应该把它包括在你的主要 urls.py.