django 获取django template中获取当前url

前端之家收集整理的这篇文章主要介绍了django 获取django template中获取当前url前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在实际开发中,经常需要在template中获取当前url。获取方法如下:

django 1.9版本以上

## template文件中
{{ request.path }}  #          不带request.get参数 
{{ request.get_full_path }}  # 带request.get参数

django 1.9之前的版本

## settings.py
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

TEMPLATE_CONTEXT_PROCESSORS = TCP + (
    'django.core.context_processors.request',)

## views.py
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)
    )


## template
{{ request.path }}


猜你在找的Django相关文章