【发布时间】:2012-08-05 19:43:59
【问题描述】:
我在 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!
}
});
美味资源:
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()
任何关于如何排序的指导将不胜感激:)
【问题讨论】:
-
您要修补的 URL 是什么? api/v1/message/ 或 api/v1/message/1/ ?
-
我正在尝试修补 /api/v1/message/1/ 仍然没有乐趣。我看到了这个post,这似乎表明我需要对 Nginx 做点什么,但对于我的生活,我找不到那是什么:-/
-
您应该开始尝试了解问题所在的“层”。如果它在您的 HTTP 服务器上或在您的应用程序中(Tastypie)。尝试删除身份验证和授权并进行一些测试。此外,将其添加到您的 Meta 类中:detail_allowed_methods = ['patch']
-
好的,我要给它一个bash。我非常确信它在 HTTP 服务器端,因为代码在我的本地机器上使用 django 测试服务器的开发环境中运行良好。只有在使用 Nginx 和 Apache 服务器的生产环境中,代码才会失败,并且只会在使用 PATCH 方法的调用中失败。其他 GET 和 POST 方法按预期工作。
-
我正在检查服务器日志,在执行 PATCH 请求时,Nginx 服务器甚至没有记录调用。它会立即将其踢出。
标签: jquery django api nginx tastypie