Django:如何在模板中获取站点的根路径? [英] Django: How to get the root path of a site in template?

查看:1371
本文介绍了Django:如何在模板中获取站点的根路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的网站设计一个回家按钮,如何在模板中找到我的网站的根路径,以便我可以这样做:

 < a href ={{root_url}}>返回首页< / a> 

或者我应该首先找出我的视图中的路径,然后将其传递给模板,一些上下文
谢谢。

解决方案

我认为这里的正确方法是使用 {%url% } 标签,我假设你的url conf中有一个根URL。



urls.py

  url(r'^ mah_root / $','someapp.views.mah_view',name ='mah_view'),

然后在您的模板中:

 < a href ={%url mah_view%}>返回首页< / a> 


I want to design a go back home button for my site and how can I get the root path of my site in the template so I can do something like this:

<a href="{{ root_url }}">Go back home</a>

Or I should first figure out the path in my views and then pass it to the template to render by some context. Thanks.

解决方案

I think the proper way here is use the {% url %} tag and I'm assuming that you have a root url in your url conf.

urls.py

url(r'^mah_root/$', 'someapp.views.mah_view', name='mah_view'),

Then in your template:

<a href="{% url mah_view %}">Go back home</a>

这篇关于Django:如何在模板中获取站点的根路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆