如何在模板中获取当前页面的URL,包括参数?

发布于 2024-09-09 03:16:30 字数 112 浏览 4 评论 0原文

有没有办法在 Django 模板中获取当前页面 URL 及其所有参数?

例如,一个模板标签将打印完整的 URL,如 /foo/bar?param=1&baz=2

Is there a way to get the current page URL and all its parameters in a Django template?

For example, a templatetag that would print a full URL like /foo/bar?param=1&baz=2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

墨落画卷 2024-09-16 03:16:30

编写一个自定义上下文处理器。例如,

def get_current_path(request):
    return {
       'current_path': request.get_full_path()
     }

在您的 TEMPLATE_CONTEXT_PROCESSORS 设置变量中添加该函数的路径,并在模板中使用它,如下所示:

{{ current_path }}

如果您想在每个请求中拥有完整的 request 对象,您可以可以使用内置的 django.core。 context_processors.request 上下文处理器,然后在模板中使用 {{ request.get_full_path }}

请参阅:

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:

{{ 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.

See:

云雾 2024-09-16 03:16:30

使用 Django 的上下文处理器构建来获取模板上下文中的请求。在设置中将 request 处理器添加到 TEMPLATE_CONTEXT_PROCESSORS

TEMPLATE_CONTEXT_PROCESSORS = (

    # Put your context processors here

    'django.core.context_processors.request',
)

并在模板使用中:

{{ request.get_full_path }}

这样您不需要自己编写任何新代码。

Use Django's build in context processor to get the request in template context. In settings add request processor to TEMPLATE_CONTEXT_PROCESSORS

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.

野味少女 2024-09-16 03:16:30

在文件 context_processors.py (或类似文件)中:

def myurl( request ):
  return { 'myurlx': request.get_full_path() }

在 settings.py 中:

TEMPLATE_CONTEXT_PROCESSORS = (
  ...
  wherever_it_is.context_processors.myurl,
  ...

在 template.html 中:

myurl={{myurlx}}

In a file context_processors.py (or the like):

def myurl( request ):
  return { 'myurlx': request.get_full_path() }

In settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
  ...
  wherever_it_is.context_processors.myurl,
  ...

In your template.html:

myurl={{myurlx}}
独孤求败 2024-09-16 03:16:30

如果我们访问以下 URL:http://127.0.0.1:8000/home/?q=test

那么

request.path = '/home/'
request.get_full_path() = '/home/?q=test'
request.build_absolute_uri() = 'http://127.0.0.1:8000/home/?q=test'

If we are acessing the following URL: http://127.0.0.1:8000/home/?q=test

then

request.path = '/home/'
request.get_full_path() = '/home/?q=test'
request.build_absolute_uri() = 'http://127.0.0.1:8000/home/?q=test'
信仰 2024-09-16 03:16:30

Django 有很多内置的东西,但是如果你不明确你想使用什么,它就不会被使用。

因此,在 MTV 模式(模型、模板、视图)中,视图接收请求并使用模板渲染来生成响应,向其传递该视图的字典或所有局部变量(使用 locals() 函数)。知道了这一点,我们可以插入来自响应的当前 url,如下所示:

views.py:

from django.shortcuts import render

def page(request):
    currentUrl = request.get_full_path()
    return render(request, 'app/page.html', locals())

然后,在模板“app/page.html”中,您只需执行以下操作即可显示我们刚刚创建的 currentUrl 变量并通过 locals() 传递:

app/template/page.html:

{{ currentUrl }}

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.

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:

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/template/page.html:

{{ currentUrl }}
独闯女儿国 2024-09-16 03:16:30

除了 sdolan 的答案:

如果您使用的是 I18N 并希望将 next 值传递给 /i18n/setlang/ 来改变当前页面的语言,那么就需要剥离掉来自完整路径的当前语言代码。类似于:

full_path = request.get_full_path()
current_path = full_path[full_path.index('/', 1):]

这假设每个路径都具有格式 /LANG_CODE/any/other/stuff/with/?param='yay' 并简单地启动 LANG_CODE 无论它是什么(例如,/en/ 将生成 /)。

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:

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 /).

沙沙粒小 2024-09-16 03:16:30

您可以查看您的网址是否与其他网址不同。

{% if 'foo/bar/' in request.get_full_path %}

You can see if your url differs from the others.

{% if 'foo/bar/' in request.get_full_path %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文