如何在 Django 模板中获取当前 URL? [英] How to get the current URL within a Django template?
本文介绍了如何在 Django 模板中获取当前 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在模板中获取当前 URL.
I was wondering how to get the current URL within a template.
说我当前的网址是:
.../user/profile/
如何将其返回到模板?
推荐答案
Django 1.9 及更高版本:
Django 1.9 and above:
## 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 模板中获取当前 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文