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

假设我当前的URL是:

.../user/profile/

如何将此返回到模板?


当前回答

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

我也在寻找在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>

其他回答

你可以使用{{request.path}}获取不带参数的url。 你可以使用{{request.get_full_path}}来获取带参数的url。

其他答案都是错的,至少对我来说是这样。请求。Path不提供完整的url,只提供相对的url,例如/paper/53。我没有找到任何合适的解决方案,所以我最终在视图中硬编码url的常量部分,然后将其与request.path连接。

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

from django.shortcuts import render

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

##template
{{ app_url }}

3 . Django > 我不改变设置或任何东西。 我在模板文件中添加了下面的代码。

{{ request.path }}  #  -without GET parameters 
{{ request.get_full_path }}  # - with GET parameters

在view.py中将请求变量传递给模板文件。

view.py:

def view_node_taxon(request, cid):
    showone = get_object_or_404(models.taxon, id = cid)
    context = {'showone':showone,'request':request}
    mytemplate  = loader.get_template('taxon/node.html')
    html = mytemplate.render(context)
    return HttpResponse(html)

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

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