[英]Django Tastypie User Registration
I am creating an API using Django and TastyPie. 我正在使用Django和DeliciousPie创建API。 I am trying to register a user via a resource. 我正在尝试通过资源注册用户。 I took most of my code from this question that has a similar goal: 我从这个具有相似目标的问题中提取了大部分代码:
How to create or register User using django-tastypie API programmatically? 如何以编程方式使用django-tastypie API创建或注册用户?
My problem is, I get a problem when registering a user. 我的问题是,注册用户时遇到问题。
The code is: 代码是:
class RegisterUserResource(ModelResource):
class Meta:
allowed_methods = ['post']
object_class = VouchersUser
authentication = Authentication()
authorization = Authorization()
include_resource_uri = False
fields = ['username']
resource_name = 'register'
def obj_create(self, bundle, request=None, **kwargs):
try:
bundle = super(RegisterUserResource).obj_create(bundle, request, **kwargs)
bundle.obj.set_password(bundle.data.get('password'))
bundle.obj.save()
except IntegrityError:
raise BadRequest('User with this username already exists')
return bundle
When I send a POST (I do it programatically) with both username and password parameters, though, I get the following error back: 但是,当我同时发送使用用户名和密码参数的POST(以编程方式进行)时,我收到以下错误消息:
{"error_message": "The format indicated 'multipart/form-data' had no available deserialization method. Please check your formats and content_types on your Serializer.", "traceback": "Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 195, in wrapper
response = callback(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 402, in dispatch_list
return self.dispatch('list', request, **kwargs)
File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 431, in dispatch
response = method(request, **kwargs)
File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 1176, in post_list
deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 351, in deserialize
deserialized = self._meta.serializer.deserialize(data, format=request.META.get('CONTENT_TYPE', 'application/json'))
File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/serializers.py", line 192, in deserialize
raise UnsupportedFormat("The format indicated '%s' had no available deserialization method. Please check your formats and content_types on your Serializer." % format)
UnsupportedFormat: The format indicated 'multipart/form-data' had no available deserialization method. Please check your formats and content_types on your Serializer.
"}
I can deduce there is some problem with the serializer but which and how can I solve it? 我可以推断出序列化器存在一些问题,但是该如何解决?
Thank you 谢谢
I guess you are trying to use django.test.client.post
with Tastypie. 我猜您正在尝试将django.test.client.post
与Deliciouspie一起使用。 If so, you need to pass in an extra parameter - the content_type. 如果是这样,则需要传入一个额外的参数-content_type。 Here is how your call should look: 这是您的通话外观:
client.post('/resource/to/create/', 'json_string_here', content_type='application/json')
had the same issue. 有同样的问题。 passing "Content-Type: application/json" in header solved it for me. 在标头中传递“ Content-Type:application / json”可为我解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.