Is there a way to get the current page URL and all its parameters in a Django template?
是否有方法在Django模板中获取当前页面URL及其所有参数?
For example, a templatetag that would print a full URL like /foo/bar?param=1&baz=2
例如,一个templatetag,它将打印完整的URL,比如/foo/bar?param=1&baz=2
6 个解决方案
#1
59
Write a custom context processor. e.g.
编写自定义上下文处理器。如。
def get_current_path(request):
return {
'current_path': request.get_full_path()
}
add a path to that function in your TEMPLATE_CONTEXT_PROCESSORS
settings variable, and use it in your template like so:
在template_context_processor设置变量中为该函数添加一个路径,并在模板中使用它,如下所示:
{{ current_path }}
If you want to have the full request
object in every request, you can use the built-in django.core.context_processors.request
context processor, and then use {{ request.get_full_path }}
in your template.
如果希望在每个请求中包含完整的request对象,可以使用内置的django.core.context_processor。请求上下文处理器,然后使用{{请求。模板中的get_full_path}。
See:
看到的:
- Custom Context Processors
- 自定义上下文处理器
- HTTPRequest's get_full_path() method.
- HTTPRequest的get_full_path()方法。
#2
21
Use Django's build in context processor to get the request in template context. In settings add request
processor to TEMPLATE_CONTEXT_PROCESSORS
使用Django在上下文处理器中的构建来获得模板上下文中的请求。在设置中,向template_context_processor添加请求处理器
TEMPLATE_CONTEXT_PROCESSORS = (
# Put your context processors here
'django.core.context_processors.request',
)
And in template use:
在模板中使用:
{{ request.get_full_path }}
This way you do not need to write any new code by yourself.
这样,您就不需要自己编写任何新代码。
#3
6
In a file context_processors.py (or the like):
在文件context_processors。py(或类似的):
def myurl( request ):
return { 'myurlx': request.get_full_path() }
In settings.py:
在settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
wherever_it_is.context_processors.myurl,
...
In your template.html:
在你template.html:
myurl={{myurlx}}
#4
1
Django has a lot of built-in stuff, but if you don't explicit what do you want to use, it won't be used.
Django有很多内置的东西,但是如果您不明确要使用什么,就不会使用它。
So, in MTV schema (Model, Template, View) the view receives a request and uses a template render to generate a response, passing on it a dictionary or all local variables (using the locals() function) of this view. Knowing this, we can insert the current url that came from the response, like this:
因此,在MTV模式(模型、模板、视图)中,视图接收请求并使用模板呈现来生成响应,并将该视图的字典或所有局部变量(使用local()函数)传递给它。了解了这一点,我们可以插入来自响应的当前url,如下所示:
views.py:
views.py:
from django.shortcuts import render
def page(request):
currentUrl = request.get_full_path()
return render(request, 'app/page.html', locals())
Then, in the template 'app/page.html' you just have to do the following to display the currentUrl variable that we just created and passed through via locals():
然后,在模板的app/页面。您只需执行以下操作,以显示我们刚刚创建并通过local()传递的currentUrl变量:
app/template/page.html:
应用程序/模板/ page.html:
{{ currentUrl }}
#5
1
In addtition to sdolan's answer:
作为对斯多兰回答的补充:
if you are using I18N and would like to pass next
value to /i18n/setlang/
to change the language of the current page, then you will need to strip off current language code from the full path either. Something like:
如果您正在使用I18N,并希望将next值传递给/ I18N /setlang/以更改当前页面的语言,那么您也需要从完整路径中删除当前语言代码。喜欢的东西:
full_path = request.get_full_path()
current_path = full_path[full_path.index('/', 1):]
This assumes that every path has format /LANG_CODE/any/other/stuff/with/?param='yay'
and simply kicks off LANG_CODE
whatever it is (e.g., /en/
will result into /
).
这里假设每条路径都有格式/LANG_CODE/其他/其他/有/?param='yay',并简单地启动LANG_CODE(例如,/en/将导致/)。
#6
0
You can see if your url differs from the others.
您可以看到您的url是否与其他url不同。
{% if 'foo/bar/' in request.get_full_path %}
#1
59
Write a custom context processor. e.g.
编写自定义上下文处理器。如。
def get_current_path(request):
return {
'current_path': request.get_full_path()
}
add a path to that function in your TEMPLATE_CONTEXT_PROCESSORS
settings variable, and use it in your template like so:
在template_context_processor设置变量中为该函数添加一个路径,并在模板中使用它,如下所示:
{{ current_path }}
If you want to have the full request
object in every request, you can use the built-in django.core.context_processors.request
context processor, and then use {{ request.get_full_path }}
in your template.
如果希望在每个请求中包含完整的request对象,可以使用内置的django.core.context_processor。请求上下文处理器,然后使用{{请求。模板中的get_full_path}。
See:
看到的:
- Custom Context Processors
- 自定义上下文处理器
- HTTPRequest's get_full_path() method.
- HTTPRequest的get_full_path()方法。
#2
21
Use Django's build in context processor to get the request in template context. In settings add request
processor to TEMPLATE_CONTEXT_PROCESSORS
使用Django在上下文处理器中的构建来获得模板上下文中的请求。在设置中,向template_context_processor添加请求处理器
TEMPLATE_CONTEXT_PROCESSORS = (
# Put your context processors here
'django.core.context_processors.request',
)
And in template use:
在模板中使用:
{{ request.get_full_path }}
This way you do not need to write any new code by yourself.
这样,您就不需要自己编写任何新代码。
#3
6
In a file context_processors.py (or the like):
在文件context_processors。py(或类似的):
def myurl( request ):
return { 'myurlx': request.get_full_path() }
In settings.py:
在settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
wherever_it_is.context_processors.myurl,
...
In your template.html:
在你template.html:
myurl={{myurlx}}
#4
1
Django has a lot of built-in stuff, but if you don't explicit what do you want to use, it won't be used.
Django有很多内置的东西,但是如果您不明确要使用什么,就不会使用它。
So, in MTV schema (Model, Template, View) the view receives a request and uses a template render to generate a response, passing on it a dictionary or all local variables (using the locals() function) of this view. Knowing this, we can insert the current url that came from the response, like this:
因此,在MTV模式(模型、模板、视图)中,视图接收请求并使用模板呈现来生成响应,并将该视图的字典或所有局部变量(使用local()函数)传递给它。了解了这一点,我们可以插入来自响应的当前url,如下所示:
views.py:
views.py:
from django.shortcuts import render
def page(request):
currentUrl = request.get_full_path()
return render(request, 'app/page.html', locals())
Then, in the template 'app/page.html' you just have to do the following to display the currentUrl variable that we just created and passed through via locals():
然后,在模板的app/页面。您只需执行以下操作,以显示我们刚刚创建并通过local()传递的currentUrl变量:
app/template/page.html:
应用程序/模板/ page.html:
{{ currentUrl }}
#5
1
In addtition to sdolan's answer:
作为对斯多兰回答的补充:
if you are using I18N and would like to pass next
value to /i18n/setlang/
to change the language of the current page, then you will need to strip off current language code from the full path either. Something like:
如果您正在使用I18N,并希望将next值传递给/ I18N /setlang/以更改当前页面的语言,那么您也需要从完整路径中删除当前语言代码。喜欢的东西:
full_path = request.get_full_path()
current_path = full_path[full_path.index('/', 1):]
This assumes that every path has format /LANG_CODE/any/other/stuff/with/?param='yay'
and simply kicks off LANG_CODE
whatever it is (e.g., /en/
will result into /
).
这里假设每条路径都有格式/LANG_CODE/其他/其他/有/?param='yay',并简单地启动LANG_CODE(例如,/en/将导致/)。
#6
0
You can see if your url differs from the others.
您可以看到您的url是否与其他url不同。
{% if 'foo/bar/' in request.get_full_path %}