简体   繁体   English

Django drf-yasg swagger 需要 header 每条路线的 LOCALE 参数

[英]Django drf-yasg swagger required header LOCALE parameter for every route

Is it possible to make header locale parameter required for every URL in application.是否可以为应用程序中的每个 URL 设置 header区域设置参数。 Can i achieve it to set it up in global configuration or in every view method and if yes how can i do this?我可以实现它以在全局配置或每个视图方法中进行设置吗?如果可以,我该怎么做?

You have not given any details on how your view looks so i'll assume it is function based not class based but this solution can easily be implemented on cbv.您尚未提供有关视图外观的任何详细信息,因此我假设它是基于函数的而不是基于类的,但是此解决方案可以很容易地在 cbv 上实现。

Making header as a part of swagger can be achieved by this:将标头作为招摇的一部分可以通过以下方式实现:

# making a header parameter

from drf_yasg import openapi

header_param = openapi.Parameter('local',openapi.IN_HEADER,description="local header param", type=openapi.IN_HEADER)

# calling it on view

@swagger_auto_schema(manual_parameters=[header_param])
@api_view(['GET', 'PUT', 'POST'])
def test_view(request, pk):

As you want it for every view, one solution is create a utils folder for making helper methods.正如您对每个视图都需要的那样,一种解决方案是创建一个utils文件夹来制作帮助方法。 create a helper method like:创建一个辅助方法,例如:

# utils.py
from drf_yasg import openapi

def get_header_params(self):
    header_param = openapi.Parameter('local',openapi.IN_HEADER,description="local header param", type=openapi.IN_HEADER)
 
    return [header_param]

with this you can call this method in your every view like:有了这个,您可以在每个视图中调用此方法,例如:

# views.py

from utils.get_header_param

@swagger_auto_schema(manual_parameters=get_header_param())
@api_view(['GET', 'PUT', 'POST'])
def test_view(request, pk):
    # your code

@swagger_auto_schema(manual_parameters=get_header_param())
@api_view(['GET', 'PUT', 'POST'])
def test_view_2(request, pk):
    # your code

for further help you can always look through the actual documentation: https://drf-yasg.readthedocs.io/en/stable/custom_spec.html#the-swagger-auto-schema-decorator如需进一步帮助,您可以随时查看实际文档: https ://drf-yasg.readthedocs.io/en/stable/custom_spec.html#the-swagger-auto-schema-decorator

If you've started this project then i'll suggest to use drf-spectacular instead of this even yasg and django also recommends it for future projects.如果你已经开始这个项目,那么我会建议使用 drf-spectacular 而不是这个,甚至 yasg 和 django 也推荐它用于未来的项目。

Tried the answer provided by Moheb, and it worked.尝试了 Moheb 提供的答案,它起作用了。 But at a later attempt, swagger UI showed the input field for the variable, but I was not able see the values in request.headers.但在稍后的尝试中,swagger UI 显示了变量的输入字段,但我看不到 request.headers 中的值。 After much exploration I realized the following: while defining a local/custom header parameter as provided in the answer, ensure that the variable name doesnot contain any underscore.经过大量探索,我意识到以下几点:在定义答案中提供的本地/自定义 header 参数时,确保变量名称不包含任何下划线。 As an example, the below code will not pass the value of the parameter since its name is local_first, instead of localfirst例如,下面的代码不会传递参数的值,因为它的名称是 local_first,而不是 localfirst

 header_param = openapi.Parameter('local_first',openapi.IN_HEADER,description="local header param", type=openapi.IN_HEADER)

So, the correct code that worked for me is所以,对我有用的正确代码是

 header_param = openapi.Parameter('localfirst',openapi.IN_HEADER,description="local header param", type=openapi.IN_HEADER)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 DRF-YASG 中为 DRF 和 DJANGO 消除 swagger-ui 中的 id 路径参数 - How to eliminate id path parameter in swagger-ui in DRF-YASG for DRF and DJANGO 自定义路径参数解析 drf-yasg 和 Django - Custom path parameter parsing drf-yasg and Django 验证Swagger API文档(drf-yasg) - Authenticating Swagger API docs (drf-yasg) 定义自定义请求 - Swagger (Drf-yasg) - Defining custom request - Swagger (Drf-yasg) 使用 django 和 drf-yasg 重用序列化程序的问题 - Problem reusing serializers with django and drf-yasg 如何使用 drf-yasg 在 django-rest-framework 中为文件上传 API 制作 swagger 模式? - How to make swagger schema for file upload API in django-rest-framework using drf-yasg? Django rest 框架 drf-yasg swagger ListField 序列化程序的多个文件上传错误 - Django rest framework drf-yasg swagger multiple file upload error for ListField serializer 如何使用 drf-yasg 更改 django 应用程序顶部栏中的 swagger 徽标和文本? - How to change swagger logo and text in the top bar in django app with drf-yasg? drf-yasg Django restframework Swagger 页面返回空白页 django 登录 href 链接 - drf-yasg Django restframework Swagger Page return blank page with django login a href link Django 2.x drf-yasg如何在自定义方法中创建API(如在swagger中) - Django 2.x drf-yasg how to create API in a custom method (like in swagger)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM