我将网站从Django 1.4升级到Django 1.9
我有一个视图将控制传递给另一个视图:
@csrf_protect
@api_view(['POST'])
@authentication_classes((SessionAuthentication,))
def preview(request, project_id, channel_type, format=None):
return build(request, project_id, channel_type, preview=True, format=format)
@csrf_protect
@api_view(['POST'])
@authentication_classes((SessionAuthentication,))
def build(request, project_id, channel_type, preview=True, builder=None, build_after=True, format=None):
pass
问题(之前从未发生过)是,当从preview()
传递到build()
时,请求对象丢失其POST内容。
如何解决这个问题?
答案 0 :(得分:8)
您可以将您在build
视图中存储的逻辑分离为两个端点使用的公共函数,而不需要任何装饰器,例如_build
- 这样,在preview
内调用的情况下,装饰器内发生的任何事情都不会发生。
@csrf_protect
@api_view(['POST'])
@authentication_classes((SessionAuthentication,))
def preview(request, project_id, channel_type, format=None):
return _build(request, project_id, channel_type, preview=True, format=format)
@csrf_protect
@api_view(['POST'])
@authentication_classes((SessionAuthentication,))
def build(request, project_id, channel_type, preview=True, builder=None, build_after=True, format=None):
return _build(request, project_id, channel_type, preview=preview, builder=builder, build_after=build_after, format=format)
def _build(request, project_id, channel_type, preview=True, builder=None, build_after=True, format=None):
pass