我想知道如何在模板中获得当前URL。

假设我当前的URL是:

.../user/profile/

如何将此返回到模板?


当前回答

在Django 3中,你需要使用url template标签:

{% url 'name-of-your-user-profile-url' possible_context_variable_parameter %}

具体示例请参见文档

其他回答

在Django 3中,你需要使用url template标签:

{% url 'name-of-your-user-profile-url' possible_context_variable_parameter %}

具体示例请参见文档

如果你也在模板中使用js,你可以这样做: 在你的js

document.addEventListener('DOMContentLoaded', function() {
    ...
    getUrl();
}

function getUrl() {
    let someDiv = document.querySelector(`#someDiv`);
    someDiv.innerHTML = window.location.href;
}

对于你的模板

...
<div id="someDiv"><div>
...

你可以像这样在模板中获取URL:

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

或通过

{{请求。如果不需要额外的参数,则使用Path}}。

hypete和Igancio的回答应该有一些精确和更正,我只是在这里总结一下整个想法,以供将来参考。

如果你需要在模板中使用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',
)

然后,如果你在响应中发送请求内容,例如:

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)
    )

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

我也在寻找在Django模板中获得当前页面的url,因为我的意图是激活主页,成员页面,联系页面,当他们被请求时所有帖子页面。

我粘贴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>

在django模板中 简单地从{{request.path}}获取当前url 获取参数为{{request.get_full_path}}的完整url

注意: 你必须在django TEMPLATE_CONTEXT_PROCESSORS中添加request