2011-10-04 88 views
2

我正在爲Django編寫我自己的自定義模板標籤,以滿足我不斷遇到的需求。如果URL正確,我需要有條件地插入表單輸入。從本質上講,我需要將下面的模板標記封裝成一個自定義標籤定義:編寫需要訪問請求對象的自定義標籤

{% if request.path != "/login/" and request.path != "/logout/" %} 
<div style="display:none;"><input type="hidden" name="redirect_url" value="{{request.path}}" /></div> 
{% endif %} 

我已經嘗試通過使一個自定義模板標籤像這樣的:

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) 

,我發現了以下錯誤:

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.    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' 

回答

5

上下文是一種映射,您應該爲其編制索引。

current_path = context['request'].path 
相關問題