【问题标题】:404 error while trying to post using django tasty pie尝试使用 django 美味派发帖时出现 404 错误
【发布时间】:2012-08-27 20:15:30
【问题描述】:

这是我的 api.py

# myapp/api.py

from django.contrib.auth.models import User
from tastypie.authorization import Authorization
from tastypie import fields
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from links.models import Link


class UserResource(ModelResource):

    class Meta:
        queryset = User.objects.all()
        resource_name = 'users'
        excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
        allowed_methods = ['post','get']
        fields = ['username']

        def obj_create(self, bundle, request=None, **kwargs):
            try:
                bundle = super(CreateUserResource, self).obj_create(bundle, request, **kwargs)
                bundle.obj.set_password(bundle.data.get('password'))
                bundle.obj.save() 
            except IntegrityError:
                raise BadRequest('That username already exists')
            return bundle

在 python shell 上输入:

  r = requests.post("http://localhost:8000/api/users/username=Puck")

并得到 404 作为错误响应。如何使用帖子创建新用户?

urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.defaults import *
from links.api import LinkResource
from links.api import UserResource
from tastypie.api import Api
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

v1_api = Api(api_name='v1')
v1_api.register(UserResource())
v1_api.register(LinkResource())




urlpatterns = patterns('',
    # Examples:


    # Uncomment the admin/doc line below to enable admin documentation
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
     (r'^api/', include(v1_api.urls)),
)

网址架构:

^admin/
^api/ ^(?P<api_name>v1)/$ [name='api_v1_top_level']
^api/ ^(?P<api_name>v1)/ ^(?P<resource_name>link)/$ [name='api_dispatch_list']
^api/ ^(?P<api_name>v1)/ ^(?P<resource_name>link)/schema/$ [name='api_get_schema']
^api/ ^(?P<api_name>v1)/ ^(?P<resource_name>link)/set/(?P<pk_list>\w[\w/;-]*)/$ [name='api_get_multiple']
^api/ ^(?P<api_name>v1)/ ^(?P<resource_name>link)/(?P<pk>\w[\w/-]*)/$ [name='api_dispatch_detail']
^api/ ^(?P<api_name>v1)/ ^(?P<resource_name>users)/$ [name='api_dispatch_list']
^api/ ^(?P<api_name>v1)/ ^(?P<resource_name>users)/schema/$ [name='api_get_schema']
^api/ ^(?P<api_name>v1)/ ^(?P<resource_name>users)/set/(?P<pk_list>\w[\w/;-]*)/$ [name='api_get_multiple']
^api/ ^(?P<api_name>v1)/ ^(?P<resource_name>users)/(?P<pk>\w[\w/-]*)/$ [name='api_dispatch_detail']
The current URL, api/v1/users/username=Puck, didn't match any of these.

【问题讨论】:

  • 你在urls.py里放了什么?

标签: python django tastypie


【解决方案1】:

您要访问的网址是:

http://localhost:8000/api/v1/users/?username=Puck

因为你声明了api_name='v1'

【讨论】:

  • 不。还是同样的错误。我实际上已经尝试了所有可能的排列/组合,但它仍然给我同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 2012-09-27
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多