我想知道如何在模板中获得当前URL。
假设我当前的URL是:
.../user/profile/
如何将此返回到模板?
我想知道如何在模板中获得当前URL。
假设我当前的URL是:
.../user/profile/
如何将此返回到模板?
当前回答
在Django 3中,你需要使用url template标签:
{% url 'name-of-your-user-profile-url' possible_context_variable_parameter %}
具体示例请参见文档
其他回答
{{请求。路径}}和{{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 %}
但是,您可以使用以下方法获得绝对URL(感谢上面的答案)
{{ request.build_absolute_uri }}
注意: 你不需要在settings.py中包含请求,它已经在那里了。
以上答案是正确的,他们给出了很好的和简短的答案。
我也在寻找在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 3中,你需要使用url template标签:
{% url 'name-of-your-user-profile-url' possible_context_variable_parameter %}
具体示例请参见文档
这是一个老问题,但如果你在使用django-registration,它可以很容易地总结出来。
在你的登录和注销链接(让我们说在你的页面标题)添加下一个参数的链接将登录或注销。您的链接应该是这样的。
<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>
这很简单,没有其他需要做的,登出时,他们将立即重定向到他们所在的页面,为了登录,他们将填写表格,然后将重定向到他们所在的页面。即使他们错误地尝试登录,它仍然可以工作。
下面的代码帮助我:
{{ request.build_absolute_uri }}