我想知道如何在模板中获取当前的URL .
说我的网址是
/user/profile/
如何将其返回到模板?
Django 1.9及以上版本:
## template {{ request.path }} {{ request.get_full_path }}
旧:
## 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 }}
您可以像这样在模板中获取网址:
<p>URL of this page: {{ request.get_full_path }}</p>
或者
{{ request.path }} 如果您不需要额外的参数 .
{{ request.path }}
一些精确和更正应该被提到hypete's和Igancio's答案,我将在这里总结一下整个想法,以供将来参考 .
如果您需要模板中的 request 变量,则 must 将'django.core.context_processors.request'添加到TEMPLATE_CONTEXT_PROCESSORS设置,默认情况下不是这样(Django 1.4) .
request
您还必须 not forget 应用程序使用的其他上下文处理器 . 因此,要将请求添加到其他默认处理器,您可以在设置中添加此请求,以避免硬编码默认处理器列表(在以后的版本中可能会更改):
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP TEMPLATE_CONTEXT_PROCESSORS = TCP + ( 'django.core.context_processors.request', )
然后,为您提供send the request contents in your response,例如:
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模板中只需从 {{request.path}} 获取当前网址获取带参数的完整网址 {{request.get_full_path}}
{{request.path}}
{{request.get_full_path}}
Note :您必须在django中添加 request TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS
我想发送到模板的完整请求有点多余 . 我是这样做的
def home(request): app_url = request.path return render(request, 'home.html', {'app_url': app_url}) ##template {{ app_url }}
其他答案是不正确的,至少在我的情况下 . request.path 不提供完整的网址,只提供相对网址,例如 /paper/53 . 我没有找到任何正确的解决方案,所以我最终硬编码视图中的网址的常量部分,然后将其与 request.path 连接 .
request.path
/paper/53
这是一个古老的问题,但如果你使用django-registration,它可以很容易地总结出来 .
在“登录和注销”链接中(例如,在页面 Headers 中),将下一个参数添加到将要登录或注销的链接中 . 您的链接应如下所示 .
<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模板中获取当前页面的URL,因为我的目的是在请求时激活 HOME page , MEMBERS page , CONTACT page , ALL POSTS page .
HOME page
MEMBERS page
CONTACT page
ALL POSTS page
我粘贴了您可以在下面看到的HTML代码段的一部分,以了解 request.path 的用法 . 您可以在 live website 中查看http://pmtboyshostelraipur.pythonanywhere.com/
live website
<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>
7 回答
Django 1.9及以上版本:
旧:
您可以像这样在模板中获取网址:
或者
{{ request.path }}
如果您不需要额外的参数 .一些精确和更正应该被提到hypete's和Igancio's答案,我将在这里总结一下整个想法,以供将来参考 .
如果您需要模板中的
request
变量,则 must 将'django.core.context_processors.request'添加到TEMPLATE_CONTEXT_PROCESSORS设置,默认情况下不是这样(Django 1.4) .您还必须 not forget 应用程序使用的其他上下文处理器 . 因此,要将请求添加到其他默认处理器,您可以在设置中添加此请求,以避免硬编码默认处理器列表(在以后的版本中可能会更改):
然后,为您提供send the request contents in your response,例如:
在django模板中
只需从
{{request.path}}
获取当前网址获取带参数的完整网址
{{request.get_full_path}}
Note :您必须在django中添加
request
TEMPLATE_CONTEXT_PROCESSORS
我想发送到模板的完整请求有点多余 . 我是这样做的
其他答案是不正确的,至少在我的情况下 .
request.path
不提供完整的网址,只提供相对网址,例如/paper/53
. 我没有找到任何正确的解决方案,所以我最终硬编码视图中的网址的常量部分,然后将其与request.path
连接 .这是一个古老的问题,但如果你使用django-registration,它可以很容易地总结出来 .
在“登录和注销”链接中(例如,在页面 Headers 中),将下一个参数添加到将要登录或注销的链接中 . 您的链接应如下所示 .
这就是它,没有其他任何事情需要做,在注销时它们会立即被重定向到它们所在的页面,为了登录,它们将填写表格,然后它将重定向到它们所在的页面 . 即使他们错误地尝试登录它仍然有效 .
以上答案是正确的,他们给出了很好的答案 .
我也想在Django模板中获取当前页面的URL,因为我的目的是在请求时激活
HOME page
,MEMBERS page
,CONTACT page
,ALL POSTS page
.我粘贴了您可以在下面看到的HTML代码段的一部分,以了解
request.path
的用法 . 您可以在live website
中查看http://pmtboyshostelraipur.pythonanywhere.com/