2013-08-21 87 views
0

我正在學習Django的過程中,目前我正在創建自己的模板標籤。我試圖創建一些基於菜單項的字符串來創建菜單。在Django的模板語言,下面會產生什麼樣的我在尋找:{在menu_items%%,持續項目}Django節點屬性錯誤

{{項目}} {%如果不forloop.last%}

| {%ENDIF%}

{%ENDFOR%}

這是我嘗試輸入指定的templatetags應用:

def construct_menu(parser, token): 
    try: 
    tag_name, menu_items = token.split_contents() 
    except ValueError: 
    msg = '%r tag requires a space separated quote of strings.' % token.split_contents()[0] 
    raise template.TemplateSyntaxError(msg) 
    return MenuNode(menu_items[1:-1]) 

class MenuNode(template.Node): 
    def __init__(self, menu_items): 
    self.menu_items = str(self.menu_items) 

    def render(self, context): 
    ... 

register.tag('menu', construct_menu) 

這裏就是我得到的錯誤:

Environment: 


Request Method: GET 
Request URL: http://127.0.0.1:8000/news/ 

Django Version: 1.5.1 
Python Version: 2.7.2 
Installed Applications: 
('django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.sites', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'news', 
'showroom', 
'contact', 
'templatetags') 
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 /Users/per/Documents/Tilde/templates/home.html, error at line 1 
    'MenuNode' object has no attribute 'menu_items' 
    1 : {% extends "base.html" %} 


    2 : 


    3 : {% block title %} 


    4 : Home page! :D 


    5 : {% endblock %} 


    6 : for post in posts 


    7 :  post 


    8 : endfor 


    9 : {% block content %} 


    10 : This is where the news goes. 


    11 : {% endblock %} 

Traceback: 
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response 
    115.       response = callback(request, *callback_args, **callback_kwargs) 
File "/Users/per/Documents/Tilde/news/views.py" in home 
    6. return render(request, 'home.html') 
File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py" in render 
    53.  return HttpResponse(loader.render_to_string(*args, **kwargs), 
File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string 
    177.   return t.render(context_instance) 
File "/Library/Python/2.7/site-packages/django/template/base.py" in render 
    140.    return self._render(context) 
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render 
    134.   return self.nodelist.render(context) 
File "/Library/Python/2.7/site-packages/django/template/base.py" in render 
    830.     bit = self.render_node(node, context) 
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node 
    74.    return node.render(context) 
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render 
    102.   compiled_parent = self.get_parent(context) 
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in get_parent 
    99.   return get_template(parent) 
File "/Library/Python/2.7/site-packages/django/template/loader.py" in get_template 
    146.  template, origin = find_template(template_name) 
File "/Library/Python/2.7/site-packages/django/template/loader.py" in find_template 
    135.    source, display_name = loader(name, dirs) 
File "/Library/Python/2.7/site-packages/django/template/loader.py" in __call__ 
    43.   return self.load_template(template_name, template_dirs) 
File "/Library/Python/2.7/site-packages/django/template/loader.py" in load_template 
    49.    template = get_template_from_string(source, origin, template_name) 
File "/Library/Python/2.7/site-packages/django/template/loader.py" in get_template_from_string 
    157.  return Template(source, origin, name) 
File "/Library/Python/2.7/site-packages/django/template/base.py" in __init__ 
    125.   self.nodelist = compile_string(template_string, origin) 
File "/Library/Python/2.7/site-packages/django/template/base.py" in compile_string 
    153.  return parser.parse() 
File "/Library/Python/2.7/site-packages/django/template/base.py" in parse 
    274.      compiled_result = compile_func(self, token) 
File "/Users/per/Documents/Tilde/templatetags/templatetags/tagtest.py" in construct_menu 
    34.  return MenuNode(menu_items[1:-1]) 
File "/Users/per/Documents/Tilde/templatetags/templatetags/tagtest.py" in __init__ 
    38.   self.menu_items = str(self.menu_items) 

Exception Type: AttributeError at /news/ 
Exception Value: 'MenuNode' object has no attribute 'menu_items' 

我基本上是從http://www.djangobook.com/en/2.0/chapter09.html複製整個東西,但這個錯誤對我沒有意義。我想我會將所有有效的屬性傳遞給相關的地方。有什麼建議?

非常感謝提前!

回答

0

回溯表明您,這裏發生了錯誤:

self.menu_items = str(self.menu_items) 

但是,這是__init__方法的第一行。 self.menu_items尚不存在,因爲您尚未分配它。我希望你的意思是:

self.menu_items = str(menu_items) 

(雖然,因爲你可能會在某個階段通過menu_items迭代,我不知道爲什麼你想將它轉換爲字符串)。