[英]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.