django - 设置中的 Django FORCE_SCRIPT_NAME
问题描述
我正在努力寻找以下代码的作用的信息:
if set_prefix:
set_script_prefix(
'/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME
)
什么是线程本地 URL 解析器脚本?是否设置了这些脚本的前缀?它有什么意义,和配置网络服务器和环境变量有关系吗?
from django.utils.version import get_version
VERSION = (3, 0, 1, 'alpha', 1)
__version__ = get_version(VERSION)
[docs]def setup(set_prefix=True):
"""
Configure the settings (this happens as a side effect of accessing the
first setting), configure logging and populate the app registry.
Set the thread-local urlresolvers script prefix if `set_prefix` is True.
"""
from django.apps import apps
from django.conf import settings
from django.urls import set_script_prefix
from django.utils.log import configure_logging
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
if set_prefix:
set_script_prefix(
'/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME
)
apps.populate(settings.INSTALLED_APPS)
解决方案
文档没有说太多,但说:
这将用作任何 HTTP 请求中的 SCRIPT_NAME 环境变量的值
我的理解是它是一个变量,可以包含某些脚本使用的脚本根的全局路径。如果需要,可以在 settings.py 中设置。
就我而言,我收到了来自 Swagger (drf_yasg) 的警告消息:
path component of api base URL https://example.com/ is ignored; use FORCE_SCRIPT_NAME instead
为了解决这个小问题,我做了以下设置:
urls.py(api):
schema_view = get_schema_view(
openapi.Info(
title="Super API",
default_version='v1',
description="",
terms_of_service="https://example.com/terms-of-service/",
contact=openapi.Contact(email="dev@example.com"),
license=openapi.License(name="BSD License"),
),
url='https://example.com', # <========
public=True,
permission_classes=(permissions.AllowAny,),
)
设置.py:
FORCE_SCRIPT_NAME = '/'
这导致不再有此警告,并且 Swagger 显示正确的基本 URL。
希望这会有所帮助。
推荐阅读
- r - 如何在不保存结果的情况下将一个 dplyr 链的结果用于另一个
- regex - boost regex - 如何匹配一个范围内的偶数位数
- microsoft-cognitive - 401 由于印度中部地区的订阅密钥无效,访问被拒绝
- c# - 我没有收到错误,但代码没有按应有的方式工作
- python-3.x - 如何将 if、elif 条件中具有多个语句的 for 循环转换为 List Comprehensions
- python - 查找和删除 CSV 文件中的重复项
- php - 多个文件:提交后记住输入值
- intellij-idea - 如何在 Intellij Idea 上创建的 JavaFX 包中添加 JVMOptions
- python - 是否有任何方法可以在测试数据集范围之外生成股票价格预测?
- spring - Gradle 5:无法应用插件 [id 'aspectj']