2010-05-21 162 views
225

我想知道如何在模板中获取当前URL。如何获取Django模板中的当前URL?

说我的网址是

/user/profile/ 

怎样退还给这个模板?

+2

[模板读取光路(http://stackoverflow.com/questions/2127937/reading-path-in-templates) – 2014-06-19 08:53:45

+2

的可能重复看到它在我live website所有下面的答案让我觉得我需要做一些体操来访问模板中的'request'。在Django 1.10中,我只需访问模板中的{{request.path}}',它就可以工作。默认情况下'django.core.context_processors.request'已经在settings.py中配置好了,如果你使用'startproject' – User 2017-02-21 11:48:25

回答

105

的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 }} 
+2

有点简单,并且不正确。它是'render_to_response',而不是'render_to_request'。你不能像在settings.py中那样定义'TEMPLATE_CONTEXT_PROCESSORS',而没有提到可能在模板中使用的其他默认处理器! – RedGlyph 2012-04-15 14:05:35

+8

截至2016年,您不再需要为views.py添加任何内容。只要在TEMPLATE_CONTEXT_PROCESSORS中加载了django.core.context_processors.request,您就可以从模板中访问{{request.path}}。 – Routhinator 2016-04-24 20:14:03

+5

'request.path'不包含像'?foo = bar'这样的查询参数。改为使用'request.get_full_path'。 – Flimm 2016-12-07 13:32:55

264

您可以在模板中获取URL是这样的:

<p>URL of this page: {{ request.get_full_path }}</p> 

{{ request.path }}如果y你不需要额外的参数。

一些精度和更正应该带到hypete'sIgancio'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) 
    ) 
+4

我使用了一个扩展的泛型类视图,并且没有必要在上下文中添加'request'。 – Bobort 2012-06-04 16:32:37

+0

绝对清洁以避免硬编码TCP列表,但docs.djangoproject.com/en/dev/topics/settings/#default-settings 说:'请注意,设置文件不应该从global_settings导入,因为这是多余的' – Medorator 2014-04-06 19:47:22

+3

'return request(request,'user/profile.html',{'title':'User profile'})'更短 – 2014-06-26 10:43:37

1

这是一个老问题,但它可以一样容易这一点,如果你使用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> 

这就是简单地将它,没有别的需要做的事情,退出时他们会立即被重定向到他们的页面中,日志中,他们将填写表格,然后它会重定向到页面他们在上。即使他们错误地尝试登录它仍然有效。

+3

如果路径在url中,则应对其进行编码:'{{request.path | urlencode}}' – Quentin 2015-07-13 11:26:05

5

在Django模板
只需从{{request.path}}
获取当前的URL获得完整的URL与参数{{request.get_full_path}}

注意: 你必须在Django添加requestTEMPLATE_CONTEXT_PROCESSORS

4

我想发送到模板全请求有点多余。我这样做

def home(request): 
    app_url = request.path 
    return render(request, 'home.html', {'app_url': app_url}) 

##template 
{{ app_url }} 
2

其他答案是不正确的,至少在我的情况。 request.path不提供完整的网址,只有相对的网址,例如/paper/53。我没有找到任何合适的解决方案,所以我最终在视图中对URL中的常量部分进行了硬编码,然后将它与request.path连接起来。

+0

查看日期。答案是在6或7年前给出的。 – dotty 2017-06-15 10:42:43

0

以上答案是正确的,他们给出了很好和简短的答案。

我也一直在寻找获得当前页面的URL在Django模板作为我的意图是要激活HOME pageMEMBERS pageCONTACT pageALL 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>