Django:站点范围的 URL 前缀

发布于 2024-09-03 06:30:21 字数 294 浏览 6 评论 0原文

我已经构建了一个 Django 站点,该站点上线后将位于根目录下。现在它在 IP 地址上运行良好。出于测试目的,客户端指向了一个代理 url,但该 url 中包含 /folder/path,因此没有任何 URL 模式匹配。我把(/文件夹/路径)?到所有 url 模式中,以便它们现在响应,但所有链接都被破坏,因为我使用的是 {% url %} 标记,虽然 url 模式将匹配可选路径,但它们不将其包含在该标记中。

显然,我可以将 /folder/path 硬编码到我的所有 url 中(好吧,写入所有 url 包含的内容),直到测试完成,但是有更好的方法吗?

I've built a Django site that will live at the root when it's live. Right now it's functioning perfectly at the IP address. For testing purposes, the client has pointed a proxy url at it, but the url has /folder/path in it, so none of the URL patterns match. I put (/folder/path)? into all the url patterns so they now respond, but all of the links are broken because I'm using the {% url %} tag and while the url patterns will match the optional path, they don't include it in that tag.

Clearly I can just hard-code /folder/path into all of my urls (well, into all of the url includes) until testing is complete, but is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏九 2024-09-10 06:30:21

您可以在部署应用程序时通过在 Apache 配置中正确设置 WSGIScriptAlias 来管理此操作(假设您使用的是 mod_wsgi,您应该这样做)。这会传递给 Django,然后 Django 会自动为所有 URL 反向查找添加正确的值前缀。您不需要对前缀进行任何手动处理。

You manage this when you deploy your application, by correctly setting the WSGIScriptAlias in your Apache configuration (assuming you're using mod_wsgi, which you should be doing). This is passed on to Django, which then automatically prefixes all URL reverse lookups with the correct value. You shouldn't need to do any manual mucking about with prefixes.

千柳 2024-09-10 06:30:21

为此,我在 settings.py 中使用 URL_PREFIX 并将其添加到 urls.py 的每个包含中。我还将其添加到 MEDIA_URL 的开头,以便所有图像/css/js 链接都能正常工作。但我还想听听一些更棘手的解决方案?

For this purpouse I use URL_PREFIX in settings.py and add it in each include in urls.py. I also add it at the beginning of MEDIA_URL, for all images/css/js links to work. But I would also like to hear about some more tricky solution?

天邊彩虹 2024-09-10 06:30:21

如果 WSGIScriptAlias 不起作用或者由于某种原因无法设置它,请记住 urlconf 中的 include() 函数会将前缀添加到所有 URL。您可以创建一个包含当前根 urlconf 的 urlconf 并将其挂载在前缀处,然后将 settings.py 文件指向该 urlconf。

免责声明:我自己还没有尝试过,但它应该有效。

if WSGIScriptAlias doesn't work or you can't set it up for some reason, remember that the include() function in a urlconf adds the prefix to all URLs. you can create an urlconf which includes your current root urlconf and mount it at the prefix, and then point your settings.py file to that urlconf.

Disclaimer: haven't tried this myself, but it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文