Note that there are some explanatory texts on larger screens.

plurals
  1. PO'NoneType' object has no attribute 'source' error while rendering custom template tag
    primarykey
    data
    text
    <p>Edit:I debugged the traceback after lot of time. Look at the end of the question for the answer.</p> <p>I wrote a custom template tag that checks if a particular group on a site is bookmarked by a user and adds specific content to html based on that. </p> <p>The tempplate tag I wrote is as follows.</p> <pre><code>def do_if_bookmarked(parser, token): bits = token.contents.split() if len(bits) != 3: raise template.TemplateSyntaxError("%s tag takes two arguments" % bits[0]) nodelist_true = parser.parse(('else', 'endif_bookmarked')) token = parser.next_token() if token.contents == 'else': nodelist_false = parser.parse(('endif_bookmarked',)) parser.delete_first_token() else: nodelist_false = template.NodeList() return IfBookmarkedNode(bits[1], bits[2], nodelist_true, nodelist_false) class IfBookmarkedNode(template.Node): def __init__(self, user, pageuri, nodelist_true, nodelist_false): self.nodelist_true = nodelist_true self.nodelist_false = nodelist_false self.user = template.Variable(user) self.pageuri = template.Variable(pageuri) def render(self, context): try: user = self.user.resolve(context) pageuri = self.pageuri.resolve(context) except template.VariableDoesNotExist: return ' ' if BookmarkTag.objects.filter(user__pk=user.id, pageuri=pageuri): return self.nodelist_true.render(context) else: return self.nodelist_false.render(context) register = template.Library() register.tag('if_bookmarked', do_if_bookmarked) </code></pre> <p>and the html part is </p> <pre><code>{% if user.is_authenticated %} {% if_bookmarked user pageuri %} &lt;p&gt;you bookmared this&lt;/p&gt; {% else %} &lt;form method="post" {{bookmarkformaction}} &gt; details:&lt;textarea rows="2" cols="20"name='bookmarkdetails'&gt; &lt;/textarea&gt; name:&lt;input type="text" id="id_bookmarkname" name='bookmarkname' value="" /&gt; &lt;input type="submit" name="add-bookmark" value="Add Bookmark" /&gt; &lt;/form&gt; {% endif_bookmarked %} {% else %} &lt;a href="/accounts/login/"&gt;login&lt;/a&gt; &lt;a href="/accounts/register/"&gt;register&lt;/a&gt; {% endif %} </code></pre> <p>I understand that node object is 'None' from the traceback but I am puzzled as to how to solve this. What is it I am overlooking?</p> <p>Please let me know if the info is inadequeate or if question is not clear. I will amend it as required.</p> <p>traceback:</p> <pre><code> Environment: Request Method: GET Request URL: http://localhost:8000/posttags/page/-EE-Btech/ Django Version: 1.3.1 Python Version: 2.7.2 Installed Applications: ['registration_defaults', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'registration', 'debug_toolbar', 'postpots'] Installed Middleware: ('debug_toolbar.middleware.DebugToolbarMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware') Traceback: File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/home/Qalqi/Projects/Python/Django/djangopath/postpots/views.py" in page 42. context_instance=RequestContext(request)) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django /shortcuts/__init__.py" in render_to_response 20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string 181. t = get_template(template_name) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/loader.py" in get_template 157. template, origin = find_template(template_name) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/loader.py" in find_template 134. source, display_name = loader(name, dirs) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/loader.py" in __call__ 42. return self.load_template(template_name, template_dirs) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/loader.py" in load_template 48. template = get_template_from_string(source, origin, template_name) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/loader.py" in get_template_from_string 168. return Template(source, origin, name) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/debug_toolbar/panels/template.py" in new_template_init 37. old_template_init(self, template_string, origin, name) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/base.py" in __init__ 108. self.nodelist = compile_string(template_string, origin) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/base.py" in compile_string 136. return parser.parse() File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/base.py" in parse 239. compiled_result = compile_func(self, token) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/defaulttags.py" in do_if 922. nodelist_true = parser.parse(('else', 'endif')) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/base.py" in parse 243. self.extend_nodelist(nodelist, compiled_result, token) File "/home/Qalqi/.virtualenvs/sandmaze/local/lib/python2.7/site-packages/django/template/debug.py" in extend_nodelist 58. node.source = token.source Exception Type: AttributeError at /posttags/page/-EE-Btech/ Exception Value: 'NoneType' object has no attribute 'source' </code></pre> <p>EDit: This problem occcured because the do_if_bookmarked method was not returning any object in some cases. Make sure you have some object that is beign returned.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload