pis*_*hio 3 python django django-rest-framework
我正在将一个站点从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
Run Code Online (Sandbox Code Playgroud)
问题(之前从未发生过)是,当从中传递preview()时build(),请求对象丢失其POST内容.
怎么解决这个?
您可以将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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
228 次 |
| 最近记录: |