gpt4 book ai didi

django - 如何获取 Django 模板中的当前 URL?

转载 作者:行者123 更新时间:2023-11-28 19:32:06 25 4
gpt4 key购买 nike

我想知道如何在模板中获取当前 URL。

假设我当前的 URL 是:

.../user/profile/

如何将其返回到模板?

最佳答案

Django 1.9 及以上版本:

## template
{{ request.path }} # -without GET parameters
{{ request.get_full_path }} # - with GET parameters

旧的:

## 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 }}

关于django - 如何获取 Django 模板中的当前 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2882490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com