如何在Django模板中获取当前URL?
我想知道如何在模板中获取当前URL 假设我的当前URL是:如何在Django模板中获取当前URL?,django,django-templates,Django,Django Templates,我想知道如何在模板中获取当前URL 假设我的当前URL是: ../user/profile/ 如何将其返回到模板?Django 1.9及以上版本: ## template {{ request.path }} # -without GET parameters {{ request.get_full_path }} # - with GET parameters 旧的: 您可以在模板中获取URL,如下所示: <p>URL of this page: {{ request.
../user/profile/
如何将其返回到模板?Django 1.9及以上版本:
## template
{{ request.path }} # -without GET parameters
{{ request.get_full_path }} # - with GET parameters
旧的:
您可以在模板中获取URL,如下所示:
<p>URL of this page: {{ request.get_full_path }}</p>
然后,假设您,例如:
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注册,它可以像这样简单地总结 在您的登录和注销链接中(比如在页面标题中),向链接添加下一个参数,该参数将转到登录或注销。你的链接应该是这样的
<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>
简单地说,不需要做任何其他事情,一旦注销,他们将立即重定向到他们所在的页面,登录时,他们将填写表格,然后重定向到他们所在的页面。即使他们错误地尝试登录,它仍然有效 django模板中的只需从
{{request.path}
用于获取带参数的完整url
{{request.get\u full\u path}
注意:
您必须在django
TEMPLATE\u CONTEXT\u处理器中添加request
,我想发送到模板的完整请求有点多余。我是这样做的
from django.shortcuts import render
def home(request):
app_url = request.path
return render(request, 'home.html', {'app_url': app_url})
##template
{{ app_url }}
其他答案是不正确的,至少对我来说是这样<代码>请求。路径
不提供完整的url,只提供相对url,例如/paper/53
。我没有找到任何合适的解决方案,因此在将url的常量部分与请求连接之前,我最终对视图中的url进行了硬编码。path
上面的答案是正确的,它们给出了非常简短的答案
我还希望在Django模板中获取当前页面的url,因为我的目的是在请求时激活主页
,成员页面
,联系人页面
,所有帖子页面
我正在粘贴HTML代码片段的一部分,您可以在下面看到它,以了解request.path
的用法。你可以在我的直播网站上看到它
{%if”/“==request.path%}
-
{%else%}
-
{%endif%}
{%if”/members/“==request.path%}
-
{%else%}
-
{%endif%}
{%if”/contact/“==request.path%}
-
{%else%}
-
{%endif%}
{%if”/posts/“==request.path%}
-
{%else%}
-
{%endif%}
下面的代码帮助我:
{{ request.build_absolute_uri }}
{request.path}和{{request.get_full_path}
都返回当前URL,但不是绝对URL,例如:
您的网站www.com/wallpapers/new\u wallper
两者都将返回/new\u壁纸/
(注意前面和后面的斜杠)
所以你必须做一些类似的事情
{% if request.path == '/new_wallpaper/' %}
<button>show this button only if url is new_wallpaper</button>
{% endif %}
注:
您不必在设置.py中包含请求
,它已经存在了。在Django 3中,您要使用模板标记:
{% url 'name-of-your-user-profile-url' possible_context_variable_parameter %}
例如,对于Django>3
我不改变设置或任何事情。
我在模板文件中添加了以下代码
{{ request.path }} # -without GET parameters
{{ request.get_full_path }} # - with GET parameters
并在view.py中将请求变量传递到模板文件
{{ request.path }} # -without GET parameters
{{ request.get_full_path }} # - with GET parameters
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)
您可以使用{{request.path}获取不带参数的url
您可以通过使用{{request.get_full_path}}使用示例:
<!-- BRAND -->
<div class="brand">
<div class="logo">
{% if request.get_full_path == '/' %}
<a href="{% url 'front:homepage' %}" class="first-logo big-logo">
<img src="{% static 'assets/images/logo-big.svg' %}"
alt="pozitiv">
</a>
<a href="{% url 'front:homepage' %}" class="second-logo mobile-logo">
<img src="{% static 'assets/images/logo.svg' %}"
alt="<?php echo WEBSITE_NAME; ?>" >
</a>
{% else %}
<a href="{% url 'front:homepage' %}">
<img src="{% static 'assets/images/logo.svg' %}"
alt="<?php echo WEBSITE_NAME; ?>" style="width: 320px; margin: -20px 0 0 0;">
</a>
{% endif %}
</div>
</div>
{%if request.get_full_path=='/'%}
{%else%}
{%endif%}
有点简洁,不正确。它是render\u to\u响应
,而不是render\u to\u请求
。您不能像在settings.py中那样定义TEMPLATE\u CONTEXT\u处理器
,而不提及模板中可能使用的其他默认处理器!从2016年起,您不再需要向views.py添加任何内容。只要django.core.context\u processors.request加载到模板\u context\u processors中,您就可以从模板访问{{request.path}。request.path
不包括像?foo=bar
这样的查询参数。使用请求。改为获取完整路径。@Routhinator同意您的意见。但是很高兴知道需要包含这些中间件才能实现这一点。正如@Quentin和@user在其他回答中所评论的,如果您计划在链接中使用它,您应该包含urlencode
,通常是:../accounts/login?next={{request.get_full_path | urlencode}…
我使用了一个扩展的通用类视图,而且没有必要将请求
添加到上下文中。为了避免对TCP列表进行硬编码,显然更干净,但是docs.djangoproject.com/en/dev/topics/settings/#默认设置说:请注意,设置文件不应从全局设置导入,因为这是多余的返回渲染(请求,'user/profile.html',{'title':'user profile'})
是包含urlencode的较短成员,即{{{request.get_full_path | urlenode}
如果您正在重定向如何从get_full_path获取参数??以下所有答案可能重复,这让我觉得我需要做一些练习才能在模板中访问请求
。在Django 1.10中,我只需访问{request.path}
在模板中运行。默认情况下,django.core.context\u processors.request
已在settings.py中配置,如果
{{ request.path }} # -without GET parameters
{{ request.get_full_path }} # - with GET parameters
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)
<!-- BRAND -->
<div class="brand">
<div class="logo">
{% if request.get_full_path == '/' %}
<a href="{% url 'front:homepage' %}" class="first-logo big-logo">
<img src="{% static 'assets/images/logo-big.svg' %}"
alt="pozitiv">
</a>
<a href="{% url 'front:homepage' %}" class="second-logo mobile-logo">
<img src="{% static 'assets/images/logo.svg' %}"
alt="<?php echo WEBSITE_NAME; ?>" >
</a>
{% else %}
<a href="{% url 'front:homepage' %}">
<img src="{% static 'assets/images/logo.svg' %}"
alt="<?php echo WEBSITE_NAME; ?>" style="width: 320px; margin: -20px 0 0 0;">
</a>
{% endif %}
</div>
</div>