Constant name error undefined in custom template tags

26 views
Skip to first unread message

Kayode Odeyemi

unread,
Oct 9, 2011, 3:26:26 PM10/9/11
to django-users
Hello,

I'm creating a template tag that will will allow session variables stored as strings or
dict in a view to be available in its template. The syntax is:

{% session_value [view_name] [session_variable] [arg] %}

But at the moment I don't know how I can get Django to stop throwing name errors
on variables/class names that are available. In my code, kwargs, NoReverseMatch are all available, but
Django keeps reporting name errors on them.

Here's the snippet of the code at https://github.com/charyorde/session_value/blob/master/templatetags/tags.py

bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    view_name = bits[1]
    session_variable = '' # or args - bits at session_variable
    kwargs = None # bits as argument to session_variable
   
    return SessionNode(view_name, session_variable, kwargs, legacy_view_name=True)
session_value = register.tag(session_value)
   
class SessionNode(template.Node):
    def __init__(self, view_name, session_variable, kwargs=None, legacy_view_name=True): #session_variable is used as args
        self.view_name = view_name
        self.session_variable = session_variable
        #self.args = args
        if kwargs is not None:
            self.kwargs = kwargs
        if not legacy_view_name:
            self.legacy_view_name = legacy_view_name

I will like to know what I'm doing wrong. How do I prevent these constant name errors whenever the
template tag is registered?

Thanks
--
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde

Daniel Roseman

unread,
Oct 9, 2011, 3:49:19 PM10/9/11
to django...@googlegroups.com
On Sunday, 9 October 2011 20:26:26 UTC+1, Kayode Odeyemi wrote:
Hello,

I'm creating a template tag that will will allow session variables stored as strings or
dict in a view to be available in its template. The syntax is:

{% session_value [view_name] [session_variable] [arg] %}

But at the moment I don't know how I can get Django to stop throwing name errors
on variables/class names that are available. In my code, kwargs, NoReverseMatch are all available, but
Django keeps reporting name errors on them.

Here's the snippet of the code at https://github.com/charyorde/session_value/blob/master/templatetags/tags.py

<snip>
 
I will like to know what I'm doing wrong. How do I prevent these constant name errors whenever the
template tag is registered?

Thanks

Look again at the format of the code at that link. It clearly shows what is wrong: the try/except block starting at line 58 is badly indented, so it is not counting as part of the `render` method.

This appears to be because you have mixed tabs and spaces. *Never* do that. In fact, *never* use tabs - always use spaces.

Plus, line 37 is unnecessary - in fact harmful, because you've already registered the tag with the decorator in line 6. I don't know what happens if you decorate a function twice with the same decorator, but it's unlikely to be helpful.

(I won't go into whether or not the whole tag is just an unnecessary rewrite of `request.session.varname`, or your misunderstanding that session variables are in some way related to view names, so we'll leave it there.)
--
DR.

Kayode Odeyemi

unread,
Oct 10, 2011, 1:31:39 PM10/10/11
to django...@googlegroups.com
On Sun, Oct 9, 2011 at 8:49 PM, Daniel Roseman <dan...@roseman.org.uk> wrote:
On Sunday, 9 October 2011 20:26:26 UTC+1, Kayode Odeyemi wrote:
Hello,

I'm creating a template tag that will will allow session variables stored as strings or
dict in a view to be available in its template. The syntax is:

{% session_value [view_name] [session_variable] [arg] %}

But at the moment I don't know how I can get Django to stop throwing name errors
on variables/class names that are available. In my code, kwargs, NoReverseMatch are all available, but
Django keeps reporting name errors on them.

Here's the snippet of the code at https://github.com/charyorde/session_value/blob/master/templatetags/tags.py

<snip>
 
I will like to know what I'm doing wrong. How do I prevent these constant name errors whenever the
template tag is registered?

Thanks

Look again at the format of the code at that link. It clearly shows what is wrong: the try/except block starting at line 58 is badly indented, so it is not counting as part of the `render` method.

This appears to be because you have mixed tabs and spaces. *Never* do that. In fact, *never* use tabs - always use spaces.

Thank you. Thank you. I really didn't noticed this. Checked with vim and found out how poorly intended it looked. I was able to fix with a couple of changed settings in the editor.

Plus, line 37 is unnecessary - in fact harmful, because you've already registered the tag with the decorator in line 6. I don't know what happens if you decorate a function twice with the same decorator, but it's unlikely to be helpful.

(I won't go into whether or not the whole tag is just an unnecessary rewrite of `request.session.varname`, or your misunderstanding that session variables are in some way related to view names, so we'll leave it there.)

I'm writing this because the in-built request.session.varname doesn't provide answer to a dict use-case. I mean I can't do this:

request.session.varname.[some-property] , assuming varname is a dict.
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/1hrQIdPBdyIJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages