阿帕奇 | Django:如何在基本 URL 后面运行网站?
我有一个基本网址。 http://baseurl.com/ 我正在尝试在其背后运行项目。例如 http://baseurl.com/mongoose/ 项目运行但 URL 无法正常工作,因为它们都 引用基本网址。因此,对于“关于我”页面,它指向 http://baseurl.com/about 而不是 http://baseurl.com/mongoose/about
这是我需要在 django 或 apache 中更改的内容吗?我就是这样 尝试做什至可能吗?
来自 IIS .net 背景的我知道,在 IIS 中,您可以在站点内“创建和应用程序”,这基本上实现了我现在试图使用 Apache 和 Django 实现的目标。
谢谢
I've got a base url. http://baseurl.com/
I'm trying to run projects on the back of it. For example
http://baseurl.com/mongoose/
The projects run but the URL don't work properly because they all
reference the base url. So for 'About Me' page it points to
http://baseurl.com/about instead of http://baseurl.com/mongoose/about
Is this something i need to change in django or apache? Is what I'm
trying to do even possible?
Coming from an IIS .net background I know that in IIS you can "Create and application" within a site which essentially does what I'm trying to achieve now with Apache and Django.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不需要做任何事情。 Apache 应该设置一个名为
SCRIPT_NAME
的请求标头,这是您的基本 URL,所有 URL 反转都会考虑到这一点。您如何在模板中创建这些 URL?
更新
所以你的问题是获取 Flatpages 的 URL。问题是动态计算 URL 的正常方法,以便它们确实考虑
SCRIPT_NAME
- 使用reverse()
函数或{% url % }
标签 - 不适用于 Flatpages,因为它们不是通过 urls.py 调度,而是通过在 404 上触发的自定义中间件调度。因此,我不使用该中间件,而是使用 urls.py 机制来调度到平面。从 settings.py 中删除 flatpagemiddleware,并在 urls.py 模式末尾 添加以下内容:
现在,在模板中,您可以执行以下操作:
并且它应该可以正常工作。
You shouldn't need to do anything. Apache is supposed to be setting a request header called
SCRIPT_NAME
, which is your base URL, and all URL reversing takes that into account.How are you creating these URLs in your templates?
Update
So your problem is with getting the URLs of Flatpages. The issue is that the normal way of calculating URLs dynamically, so that they do take
SCRIPT_NAME
into account - using thereverse()
function or the{% url %}
tag - doesn't work with Flatpages, because they are not dispatched via urls.py but via a custom middleware which fires on a 404.So instead of using that middleware, I would use the urls.py mechanism to dispatch to flatpages. Remove the flatpagemiddleware from your settings.py, and in urls.py at the end of your patterns add this:
Now, in your templates, you can do:
and it should work correctly.
检查项目中的任何
urls.py
以查看它们是否期望成为顶级。但是,如果应用程序输出类似/something
的链接,那么它将表示根目录。应用程序应该将视图/参数反转为 URL,这样您就可以移动它。如果您编写了应用程序,请查看django.core.urlresolvers
中的reverse
Check any
urls.py
in the project(s) to see if they expect to be top-level. But if the application outputs links like/something
then it's going to mean the root directory. The application should be reversing a view/parameter into a URL, which would allow you to move it around. If you wrote the apps, check outreverse
indjango.core.urlresolvers