如何获取Django模板中的当前URL?

dot*_*tty 290 django django-templates

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

说我的网址是

.../user/profile/
Run Code Online (Sandbox Code Playgroud)

如何将其返回到模板?

Red*_*yph 279

您可以像这样在模板中获取网址:

<p>URL of this page: {{ request.get_full_path }}</p>
Run Code Online (Sandbox Code Playgroud)

或者

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

应该对hypeteIgancio的答案进行一些精确和更正,我将在这里总结一下整个想法,以供将来参考.

如果您需要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',
)
Run Code Online (Sandbox Code Playgroud)

然后,只要您在回复中发送request内容,例如:

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)
    )
Run Code Online (Sandbox Code Playgroud)

  • 我使用了扩展的泛型类视图,没有必要在上下文中添加`request`. (4认同)
  • `return render(request,'user/profile.html',{'title':'User profile'})`更短 (3认同)
  • 记得要包括urlencode,即`{{request.get_full_path | urlenode}}`如果你要重定向 (2认同)

htt*_*ete 200

Django 1.9及以上版本:

## template
{{ request.path }}  #  -without GET parameters 
{{ request.get_full_path }}  # - with GET parameters
Run Code Online (Sandbox Code Playgroud)

旧:

## 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 }}
Run Code Online (Sandbox Code Playgroud)

  • 截至2016年,您不再需要向views.py添加任何内容.只要在TEMPLATE_CONTEXT_PROCESSORS中加载django.core.context_processors.request,您就可以从模板访问{{request.path}}. (8认同)
  • `request.path`不包含查询参数,如`?foo = bar`.请改用"request.get_full_path". (8认同)
  • 有点简洁,不正确.它是`render_to_response`,而不是`render_to_request`.而且你不能像在settings.py中那样定义`TEMPLATE_CONTEXT_PROCESSORS`,而没有提到可能在模板中使用的其他默认处理器! (2认同)

小智 21

下面的代码帮助我:

 {{ request.build_absolute_uri }}
Run Code Online (Sandbox Code Playgroud)

  • 这很有用,因为它还包括主机名/域。 (2认同)

Muj*_*que 8

两者都{{ request.path }} and {{ request.get_full_path }}返回当前 URL 但不返回绝对 URL,例如:

your_website.com/wallpapers/new_wallpaper

两者都会返回/new_wallpaper/ (注意开头和结尾的斜杠)

所以你必须做类似的事情

{% if request.path == '/new_wallpaper/' %}
    <button>show this button only if url is new_wallpaper</button>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

但是,您可以使用(感谢上面的答案)获取绝对 URL

{{ request.build_absolute_uri }}
Run Code Online (Sandbox Code Playgroud)

注意:您不必包含request在 中settings.py,它已经存在。


Sav*_* KP 6

在django模板中,
只需获取当前URL {{request.path}}
以获取带参数的完整URL{{request.get_full_path}}

注意:您必须添加requestdjangoTEMPLATE_CONTEXT_PROCESSORS


Rad*_*ren 5

我想发送到模板的完整请求有点多余.我是这样做的

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

##template
{{ app_url }}
Run Code Online (Sandbox Code Playgroud)