django - 将域路径添加到 django html 模板
问题描述
我正在为一个结合了 nginx 反向代理和女服务员的 django 应用程序提供服务。在 nginx 配置中,应用程序通过一个位置链接:
location /app/ {
proxy_pass http://localhost:8686/;
}
当应用程序通过waitress
端口 8686 运行时。
现在,如果我访问 domain.com/app,我的索引页面会正确提供。虽然,我的 django html 模板包含以下链接:
<p> You are not logged in.</p> <a href="/accounts/login"><button>Login</button></a>
当我按下那个按钮时,我得到
domain.com/accounts/login
但应该是
domain.com/app/accounts/login
我想知道如何更改代码,使其独立于应用程序链接的位置工作。
在urls.py
网址中包含这样的内容:
urlpatterns: = [...,
path('accounts/', include('django.contrib.auth.urls'))]
解决方案
在中定义 url urls.py
(很可能您已经这样做了),然后reverse
在模板中使用:
<a href="{% url 'foo:bar' %}"><button>Login</button></a>
然后在 nginx 中重写 URL 以使您的应用认为您正在访问/accounts/login
而不是/app/accounts/login
:
location /app/ {
rewrite ^/app(.*)$ $1 last;
proxy_pass http://localhost:8686/;
}
文件:
推荐阅读
- reactjs - 在 useEffect 中反应钩子 updateState
- vue.js - VueJS 尝试使用 2 个不同的变量从 html 访问数据
- python - 在 Django 中创建自定义用户时如何检查是否生成了 uuid 字段?
- angular - Angular 测试类似 NavigationEnd 的路由器事件
- autodesk-forge - 覆盖特定上下文菜单项的回调
- rest - WCF 服务在一段时间后停止处理请求
- node.js - 我如何在 dynamo-db 中存储前聊天机器人对话
- android - Android:使用 getPickImageChooserIntent 时相机不是一个选项
- azure-devops - 用于测试用例参数值的 Azure DevOps REST API
- azure-logic-apps - 逻辑应用将继续检查 HTTP 202 Accepted 响应多长时间?