升级到django-rest-framework 3.5.3后,不允许使用HEAD方法 [英] HEAD method not allowed after upgrading to django-rest-framework 3.5.3

查看:35
本文介绍了升级到django-rest-framework 3.5.3后,不允许使用HEAD方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将django-rest-framework从3.1.3升级到3.5.3.升级后,所有使用DefaultRouter生成url的ModelViewSet和viewsset.GenericViewSet视图不再允许HEAD方法调用.我已经搜索了发行说明和文档,却找不到任何导致HEAD不再允许的设置或更改.

We are upgrading django-rest-framework from 3.1.3 to 3.5.3. After the upgrade all of our ModelViewSet and viewsets.GenericViewSet views that utilize DefaultRouter to generate the urls no longer allow HEAD method calls. I've searched through the release notes and docs and haven't been able to find any setting or change that caused HEAD to stop being allowed.

我可以通过将DefaultRouter子类化并更改路由默认值来解决此问题,但是我认为这不是最佳或正确的解决方案.通过阅读django-rest-framework问题和文档,似乎django-rest-framework应该可以自动处理HEAD和OPTIONS方法.

I'm able to resolve this issue by subclassing the DefaultRouter and altering the route defaults, but I don't think this is the best or correct solution. From reading within django-rest-framework issues and documentation, it appears that django-rest-framework should handle HEAD and OPTIONS methods automatically.

@ detail_route,@ list_route和从ApiView派生的允许GET方法的视图自动获得HEAD和OPTION方法.

@detail_route, @list_route, and views derived from ApiView which allow the GET method are automatically gaining the HEAD and OPTION methods.

为什么升级后HEAD方法消失了?确保在我们的路由中允许使用HEAD方法的正确方法是什么?

Why has the HEAD method disappeared after this upgrade and what is the correct method of ensuring HEAD methods are allowed on our routes?

我们的路线和ModelViewSet定义非常标准,这是一条无效路线:

Our route and ModelViewSet definitions are very standard, here is a non working route:

from rest_framework.routers import DefaultRouter
from user_profile import views

router = DefaultRouter(trailing_slash=False)
router.register(r'user_names', views.UserNameView)

urlpatterns = router.urls

视图:

class UserNameView(mixins.ListModelMixin,
        mixins.RetrieveModelMixin,
        viewsets.GenericViewSet):
    queryset = User.objects.only(
        "id", "first_name", "last_name", "email",
        "mobile_phone", "photo", "is_active", "date_joined"
    ).select_related("photo").all()
    serializer_class = serializers.UserNameSerializer

邮递员对HEAD呼叫的响应:

Postman response to a HEAD call:

Status: 405 Method Not Allowed
Allow →GET, OPTIONS
Content-Type →application/json
Date →Wed, 09 Nov 2016 20:50:41 GMT
Server →WSGIServer/0.1 Python/2.7.12
Vary →Cookie
X-Frame-Options →SAMEORIGIN
x-xss-protection →1; mode=block

推荐答案

您显然依赖于3.5.0版中已删除的旧行为.

You were apparently relying on an old behavior which was removed in release 3.5.0.

# Patch this in as it's otherwise only present from 1.5 onwards
if hasattr(self, 'get') and not hasattr(self, 'head'):
    self.head = self.get

以下是相关的提交 DefaultRouter 不包含HEAD路由.您可以将其添加到 routes 中,或使用 UserNameView.as_view(actions = {'head':...})

DefaultRouter does not include a HEAD route. You can add it into the routes, or specify it explictly using UserNameView.as_view(actions={'head': ...})

这篇关于升级到django-rest-framework 3.5.3后,不允许使用HEAD方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆