Django 使用 mod_wsgi 作为子 URL
类似的问题之前已在此网站上提出过,但我对当我尝试在子网址下托管我的网站时如何替换我的网站锚标记有疑问。
例如 我的域名是 www.example.com 映射到 Django 安装的子网址是 www.example.com/2010/registration 现在,由于我的模板中的锚标记(用于链接)采用“/”(用于引用根)的形式,后接其余 url,因此链接不包含在 www.example.com 内。 因此,例如,如果我的锚标记的形式
<a href='/profile'>Profile</a>
那么我在网站上的锚标记将变为 www.example.com/profile 而不是 www.example.com/2010/registration/profile/
是否有任何可能的方法来解决这东西?
谢谢, 镍丁
Similar questions have been asked before on this site, but I had a doubt as to how my site anchor tags will be replaced when I try to host my website under a suburl.
E.g.
My domain is www.example.com
and my suburl which maps to the Django installation is www.example.com/2010/registration
Now since the anchor tags in my templates (for the links) are of the form of a '/' (to reference the root) succeeded by rest of the url the links are not contained inside www.example.com.
So, for example if my anchor tag is of the form
<a href='/profile'>Profile</a>
Then my anchor tag on the site becomes www.example.com/profile instead of becoming www.example.com/2010/registration/profile/
Is there any possible way to work around this thing ?
Thanks,
Nitin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模板中可以使用一些标签来确保添加正确的前缀。首先阅读:
http://docs.djangoproject.com/en /1.2/ref/templates/builtins/#url
There are tags which can be used in templates to ensure correct prefix added. Start by reading:
http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#url
正如 Graham 所说,在模板中使用
{% url %}
标签。在视图中,使用reverse()
函数,这是等效的。请参阅文档。As Graham says, use the
{% url %}
tag in your templates. In views, use thereverse()
function, which is equivalent. See the documentation.