在实际开发中,经常需要在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 }}