回答
的Django 1.9及以上版本:
## template
{{ request.path }}
{{ request.get_full_path }}
老:
## settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)
## views.py
from django.template import *
def home(request):
return render_to_response('home.html', {}, context_instance=RequestContext(request))
## template
{{ request.path }}
有点简单,并且不正确。它是'render_to_response',而不是'render_to_request'。你不能像在settings.py中那样定义'TEMPLATE_CONTEXT_PROCESSORS',而没有提到可能在模板中使用的其他默认处理器! – RedGlyph 2012-04-15 14:05:35
截至2016年,您不再需要为views.py添加任何内容。只要在TEMPLATE_CONTEXT_PROCESSORS中加载了django.core.context_processors.request,您就可以从模板中访问{{request.path}}。 – Routhinator 2016-04-24 20:14:03
'request.path'不包含像'?foo = bar'这样的查询参数。改为使用'request.get_full_path'。 – Flimm 2016-12-07 13:32:55
您可以在模板中获取URL是这样的:
<p>URL of this page: {{ request.get_full_path }}</p>
或
{{ request.path }}
如果y你不需要额外的参数。
一些精度和更正应该带到hypete's和Igancio's的答案,我将在这里总结整个想法,以备将来参考。
如果你需要在模板中request
变量,你必须的“django.core.context_processors.request”添加到TEMPLATE_CONTEXT_PROCESSORS
设置,默认情况下它不是(Django的1.4)。
您还必须不要忘记您的应用程序使用的其他情况下的处理器。因此,请求添加到其他默认的处理器,你可以在你的设置中添加此,以避免硬编码默认的处理器列表(也可能在以后的版本很好的改变):
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
)
然后,提供你send the request
contents in your response,例如这样的:
from django.shortcuts import render_to_response
from django.template import RequestContext
def index(request):
return render_to_response(
'user/profile.html',
{ 'title': 'User profile' },
context_instance=RequestContext(request)
)
我使用了一个扩展的泛型类视图,并且没有必要在上下文中添加'request'。 – Bobort 2012-06-04 16:32:37
绝对清洁以避免硬编码TCP列表,但docs.djangoproject.com/en/dev/topics/settings/#default-settings 说:'请注意,设置文件不应该从global_settings导入,因为这是多余的' – Medorator 2014-04-06 19:47:22
'return request(request,'user/profile.html',{'title':'User profile'})'更短 – 2014-06-26 10:43:37
这是一个老问题,但它可以一样容易这一点,如果你使用Django的登记来概括。
在你登录和注销链接(让网页标题说的)下一个参数添加到会去登录或注销的链接。你的链接应该看起来像这样。
<li><a href="http://www.noobmovies.com/accounts/login/?next={{ request.path | urlencode }}">Log In</a></li>
<li><a href="http://www.noobmovies.com/accounts/logout/?next={{ request.path | urlencode }}">Log Out</a></li>
这就是简单地将它,没有别的需要做的事情,退出时他们会立即被重定向到他们的页面中,日志中,他们将填写表格,然后它会重定向到页面他们在上。即使他们错误地尝试登录它仍然有效。
如果路径在url中,则应对其进行编码:'{{request.path | urlencode}}' – Quentin 2015-07-13 11:26:05
在Django模板
只需从{{request.path}}
获取当前的URL获得完整的URL与参数{{request.get_full_path}}
注意: 你必须在Django添加request
TEMPLATE_CONTEXT_PROCESSORS
我想发送到模板全请求有点多余。我这样做
def home(request):
app_url = request.path
return render(request, 'home.html', {'app_url': app_url})
##template
{{ app_url }}
其他答案是不正确的,至少在我的情况。 request.path
不提供完整的网址,只有相对的网址,例如/paper/53
。我没有找到任何合适的解决方案,所以我最终在视图中对URL中的常量部分进行了硬编码,然后将它与request.path
连接起来。
查看日期。答案是在6或7年前给出的。 – dotty 2017-06-15 10:42:43
以上答案是正确的,他们给出了很好和简短的答案。
我也一直在寻找获得当前页面的URL在Django模板作为我的意图是要激活HOME page
,MEMBERS page
,CONTACT page
,ALL POSTS page
在需要的时候。
我正在粘贴下面可以看到的HTML代码段的一部分,以了解request.path
的使用。您可以在http://pmtboyshostelraipur.pythonanywhere.com/
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<!--HOME-->
{% if "/" == request.path %}
<li class="active text-center">
<a href="/" data-toggle="tooltip" title="Home" data-placement="bottom">
<i class="fa fa-home" style="font-size:25px; padding-left: 5px; padding-right: 5px" aria-hidden="true">
</i>
</a>
</li>
{% else %}
<li class="text-center">
<a href="/" data-toggle="tooltip" title="Home" data-placement="bottom">
<i class="fa fa-home" style="font-size:25px; padding-left: 5px; padding-right: 5px" aria-hidden="true">
</i>
</a>
</li>
{% endif %}
<!--MEMBERS-->
{% if "/members/" == request.path %}
<li class="active text-center">
<a href="/members/" data-toggle="tooltip" title="Members" data-placement="bottom">
<i class="fa fa-users" style="font-size:25px; padding-left: 5px; padding-right: 5px" aria-hidden="true"></i>
</a>
</li>
{% else %}
<li class="text-center">
<a href="/members/" data-toggle="tooltip" title="Members" data-placement="bottom">
<i class="fa fa-users" style="font-size:25px; padding-left: 5px; padding-right: 5px" aria-hidden="true"></i>
</a>
</li>
{% endif %}
<!--CONTACT-->
{% if "/contact/" == request.path %}
<li class="active text-center">
<a class="nav-link" href="/contact/" data-toggle="tooltip" title="Contact" data-placement="bottom">
<i class="fa fa-volume-control-phone" style="font-size:25px; padding-left: 5px; padding-right: 5px" aria-hidden="true"></i>
</a>
</li>
{% else %}
<li class="text-center">
<a class="nav-link" href="/contact/" data-toggle="tooltip" title="Contact" data-placement="bottom">
<i class="fa fa-volume-control-phone" style="font-size:25px; padding-left: 5px; padding-right: 5px" aria-hidden="true"></i>
</a>
</li>
{% endif %}
<!--ALL POSTS-->
{% if "/posts/" == request.path %}
<li class="text-center">
<a class="nav-link" href="/posts/" data-toggle="tooltip" title="All posts" data-placement="bottom">
<i class="fa fa-folder-open" style="font-size:25px; padding-left: 5px; padding-right: 5px" aria-hidden="true"></i>
</a>
</li>
{% else %}
<li class="text-center">
<a class="nav-link" href="/posts/" data-toggle="tooltip" title="All posts" data-placement="bottom">
<i class="fa fa-folder-open" style="font-size:25px; padding-left: 5px; padding-right: 5px" aria-hidden="true"></i>
</a>
</li>
{% endif %}
</ul>
- 1. 如何获取模板中当前URL的本地化版本?
- 2. 如何获取当前URL
- 3. Django:获取当前页面的URL,包括参数,在一个模板中
- 4. 如何获取当前URL中的RenderAction
- 5. 如何从Django模板中获取MEDIA_URL?
- 6. 如何在模板中获取ImageField URL?
- 7. Django模板,如果基于当前URL值的标签
- 8. 如何获取GAE模板中当前页面的网址
- 9. 获取当前URL
- 10. 获取当前URL
- 11. 获取当前URL
- 12. 如何获取当前suitelet的url
- 13. Django模板能否传递参数?获取当前用户
- 14. Django模板:以另一种语言获取当前网址
- 15. 获取当前模板名称?
- 16. 如何在Django模板中集中URL?
- 17. 获取当前URL(UIWebView的)
- 18. 如何使用XMLHttpRequest获取当前URL
- 19. 如何动态获取当前基URL?
- 20. 机械化如何获取当前url
- 21. WordPress的获取模板URL
- 22. 如何获取当前执行的模板文件名?
- 23. 如何获取Joomla 1.5当前模板的路径?
- 24. Django的匹配当前URL和负载模板
- 25. 如何获取当前的Datomic模式?
- 26. 如何在Safari中获取/查看/读取当前的URL?
- 27. FreeMarker - 获取当前URL
- 28. Python:获取pywebview当前URL
- 29. 获取当前URL在Python
- 30. 如何在Django模板中获取模型的对象数?
[模板读取光路(http://stackoverflow.com/questions/2127937/reading-path-in-templates) – 2014-06-19 08:53:45
的可能重复看到它在我
live website
所有下面的答案让我觉得我需要做一些体操来访问模板中的'request'。在Django 1.10中,我只需访问模板中的{{request.path}}',它就可以工作。默认情况下'django.core.context_processors.request'已经在settings.py中配置好了,如果你使用'startproject' – User 2017-02-21 11:48:25