Search code examples
pythondjangotastypie

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


I am trying to create a Django website and more importantly, import the TastyPie API. But every time I run /articles/api/article on my localhost, I get this error message:

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.

Here is my urls.py file, which is located under my django_test/django_test directory:

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)),

)

And this is my api.py file, which is also located in my django_test/django_test directory:

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'

I want to get a different error message that says:

"Sorry, not implemented yet. Please append "?format=json" to your URL."

I'm really confused and I would appreciate any help you can give me. Thank you.


Solution

  • Your last line in urls.py should read:

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

    The article part is automatically generated by tastypie using the model name. You should not include it in your main urls.py.