我正在 Apache 上运行一个 Django 站点,该站点以 Nginx 实例为前端,为我的静态媒体提供服务。
我通过 django-tastypie 向需要修补字段的模型公开 API。当我进行本地测试(通过 django runserver)时,一切都按预期工作。然而,在实时服务器上,我收到返回的“400(错误请求)”。
我看过一些地方说Nginx不支持PATCH?是对的吗?有没有一个好的解决方法?我做错了什么吗?
我仅通过 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!
}
});
Tastypie 资源:
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()
任何有关如何对此进行排序的指示将不胜感激:)
最佳答案
如果您使用的是最新版本的 TastyPie(来自 GitHub 存储库的版本,自 8 月 5 日起),您可以按照说明 from the documentation 进行操作。 :
Using
PUT/DELETE/PATCH
In Unsupported PlacesSome 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 anX-HTTP-Method-Override
header. For example, to send aPATCH
request overPOST
, 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
header 以及您尝试执行的方法的名称。
关于jquery - django-tastypie PATCH 给了我一个 "400 (Bad Request)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11868908/