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


相关文章

注:所有源代码均实测运行过。所有源代码均已上传CSDN,请有...
继承APIView和ViewSetMixin;作用也与APIView基本类似,提供...
一、Django介绍Python下有许多款不同的 Web 框架。Django是重...
本文从nginx快速掌握到使用,gunicorn快速掌握到使用,实现小...
uniapp微信小程序订阅消息发送服务通知
Django终端打印SQL语句 1 Setting配置: 2 默认python 使用的...