I'm writing my own custom template tag for Django for a purpose I keep coming across. I need to conditionally insert a form input if the URL is right. Essentially, I need to encapsulate the following template markup into a custom tag definition:
{% if request.path != "/login/" and request.path != "/logout/" %}
<div style="display:none;"><input type="hidden" name="redirect_url" value="{{request.path}}" /></div>
{% endif %}
I've tried doing this by making a custom template tag like so:
from django import template
import re
register = template.Library()
class RedirectUrlNode(template.Node):
def render(self, context):
current_path = context.request.path
if re.match("^log(?:in|out)/?", current_path):
return ""
else:
return "..." # insert div and input elements here
def do_redirect_url(parser, token):
return RedirectUrlNode()
register.tag("redirect_url", do_redirect_url)
I'm getting the following error:
Environment:
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.staticfiles',
'myproject.web',
'reversion',
'south']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template error:
In template /home/rfkrocktk/Desktop/myproject/src/myproject/web/templates/desktop/foundation.html, error at line 24
Caught AttributeError while rendering: 'RequestContext' object has no attribute 'request'
14 : <li class="dropdown" data-dropdown="dropdown">
15 : <a class="dropdown-toggle">{{user.first_name}}</a>
16 : <ul class="dropdown-menu">
17 : <li>
18 : <a href="/me/">Preferences</a>
19 : </li>
20 : <li class="divider"></li>
21 : <li>
22 : <form id="logoutForm" method="post" action="/logout/">
23 : {% csrf_token %}
24 : {% redirect_url %}
25 : <a href="#" onclick="document.logoutForm.submit()">Log Out</a>
26 : </form>
27 : </li>
28 : </ul>
29 : </li>
30 : </ul>
31 : {% else %}
32 : <form id="loginForm" style="float:right;" action="/login/" method="post">
33 : {% csrf_token %}
34 : {% if request.path != "/login/" and request.path != "/logout/" %}<div style="display:none;"><input type="hidden" name="redirect_url" value="{{request.path}}" /></div>{% endif %}
Traceback:
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/views/generic/simple.py" in direct_to_template
28. return HttpResponse(t.render(c), mimetype=mimetype)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
123. return self._render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. 开发者_JAVA百科 result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/defaulttags.py" in render
311. return self.nodelist_true.render(context)
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/rfkrocktk/Desktop/myproject/eggs/Django-1.3-py2.7.egg/django/template/debug.py" in render_node
73. result = node.render(context)
File "/home/rfkrocktk/Desktop/myproject/src/myproject/web/templatetags/form_shortcuts.py" in render
8. current_path = context.request.path
Exception Type: TemplateSyntaxError at /
Exception Value: Caught AttributeError while rendering: 'RequestContext' object has no attribute 'request'
The context is a mapping, and you should index it.
current_path = context['request'].path
精彩评论