これは私の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シェルでそれを入力しました:
r = requests.post("http://localhost:8000/api/users/username=Puck")
エラー応答として404を取得しました。post を使用して新しいユーザーを作成するにはどうすればよいですか?
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)),
)
URL スキーマ:
^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.