问题描述
我正在 Apache 上运行一个 Django 站点,该站点以 Nginx 实例为前端来服务我的静态媒体.
I am running a Django site on Apache which is front'ed by Nginx instance to serve my static media.
我通过 django-tastypie 将 API 公开给我需要修补字段的模型.当我进行本地测试(通过 django runserver)时,一切都按预期工作.然而,在实时服务器上,我得到了400(错误请求)"的返回.
I expose an API via django-tastypie to a model that I need to PATCH a field on. When I do local testing (via the django runserver) everything works as expected. On the live server however I get "400 (Bad Request)" returned.
我读过一些地方说 Nginx 不支持 PATCH?那正确吗?有什么好的解决方法吗?我做错了什么吗?
I've read a few places saying that Nginx does not support PATCH? Is that right? Is there a good workaround for this? Am I doing something wrong?
我只通过postData发送我想要更新的字段.
I only send through the fields I want to update via the postData.
JQuery 代码:
$.ajax({url: '...',
type: 'PATCH',
accepts: 'application/json',
contentType: 'application/json',
dataType: 'json',
data: postData,
processData: false,
success: function() {
// Success Code!
},
error: function() {
// Error Code!
}
});
美味资源:
class ReceivedMessageResource(ModelResource):
"""
"""
campaign = fields.ForeignKey(CampaignResource, 'campaign')
campaign_name = fields.CharField(readonly=True)
campaign_id = fields.IntegerField(readonly=True)
message_type = fields.CharField(readonly=True)
display_date = fields.CharField(readonly=True)
attachments = fields.ToManyField('apps.campaign.api.AttachmentResource',
'attachment_set',
related_name='message',
full=True)
class Meta:
queryset = ReceivedMessage.objects.all()
resource_name = 'message'
filtering = {'id': ALL,
'campaign': ALL_WITH_RELATIONS}
excludes = ['reason', 'provider', 'loyalty_profile', 'original_message', 'date_received']
allowed_methods = ['get', 'post', 'put', 'delete', 'patch']
paginator_class = ReceivedMessagesPaginator
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
关于如何排序的任何方向将不胜感激:)
Any direction on how to sort this will be appreciated :)
推荐答案
如果您使用的是最新版本的 TastyPie(来自 GitHub 存储库,自 8 月 5 日起),您可以按照说明操作 来自文档:
If you are using the latest version of TastyPie (the one from GitHub repository, since August 5th), you can follow the instructions from the documentation:
在不支持的地方使用PUT/DELETE/PATCH
某些地方,例如某些浏览器或主机,不允许使用 PUT
/DELETE
/PATCH
方法.在这些环境中,您可以通过提供 X-HTTP-Method-Override
标头来模拟这些类型的请求.例如,要通过 POST
发送 PATCH
请求,您可以发送如下请求:
Some places, like in certain browsers or hosts, don’t allow the PUT
/DELETE
/PATCH
methods. In these environments, you can simulate those kinds of requests by providing an X-HTTP-Method-Override
header. For example, to send a PATCH
request over POST
, you’d send a request like:
curl --dump-header - -H "Content-Type: application/json" -H "X-HTTP-Method-Override: PATCH" -X POST --data '{"title": "I Visited Grandma Today"}' http://localhost:8000/api/v1/entry/1/
因此,如果您的主机不支持此方法,请添加带有您尝试执行的方法名称的 X-HTTP-Method-Override
标头.
So if your host does not support this method, add X-HTTP-Method-Override
header with the name of the method you are trying to perform.
这篇关于django-tastypie PATCH 给了我一个“400(错误请求)";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!