views:

772

answers:

2

Wondering what is causing this? Had me stumped for some time, everything checks out in console when running in pieces

as a side note: the template is using the same object in other places and displaying values - the object in template is also the same one loaded in the console below

error

'NoneType' object has no attribute 'source'

template

{% form_transaction prop %}

console value of prop

>>> prop = VacationHome.objects.get(pk=1)
>>> prop
<VacationHome: Samantha Dunn's vacation home at Close to Disney>
>>> prop.sell
0
>>> prop.rent
1
>>> count = 0
>>> string = ''
>>> type  = []
>>> num = 0
>>> for tr in TRANSACTION_MODEL:
...     if getattr(prop, tr, False):
...         type.append(count+1)
...         cur_count = count+1
...         string += '<li><label for="id_transaction_%s"><input type="checkbox" name="transaction" value="%s" id="id_transaction_%s" />%s</label></li>' % (count, cur_count, count, TRANSACTION_TITLE[count][1])
...         num += 1
...     count += 1
... 
>>> string
'<li><label for="id_transaction_1"><input type="checkbox" name="transaction" value="2" id="id_transaction_1" />Rental</label></li>'

DEFINITIONS

TRANSACTION_TITLE = (
    (1, 'Purchase'),
    (2, 'Rental'),
    (3, 'Exchange'),
)

TRANSACTION_MODEL = ['sell', 'rent', 'exchange']

template tag

@register.tag
def prop_form_transaction(parser, token):
    try:
        tag_name, prop = token.split_contents()
        count = 0
        string = ''
        type  = []
        num = 0
        for tr in TRANSACTION_MODEL:
            if getattr(prop, tr, False):
                type.append(count+1)
                cur_count = count+1
                string += '<li><label for="id_transaction_%s"><input type="checkbox" name="transaction" value="%s" id="id_transaction_%s" />%s</label></li>' % (count, cur_count, count, TRANSACTION_TITLE[count][1])
                num += 1
            count += 1

        if num:
            if num > 1:
                return string
            else:
                return '<input type="hidden" name="transaction" value="'#+str(type[0])+'" />'
    except ValueError:
            raise template.TemplateSyntaxError, "%r tag requires exactly one argument" % token.contents.split()[0]

views

def property_list_city(request, type, city):
city = str(city).replace('-', ' ')
if type == 'timeshare':
    timeshares = Timeshare.objects.filter(resort__city__icontains=city).filter(available__icontains=True)
    resorts = Resort.objects.filter(city__icontains=city)
    objects = chain(timeshares, resorts)
elif type == 'vacation_home':
    objects = VacationHome.objects.filter(city__icontains=city)
else:
    objects = False

context = {  #line 265
    'properties' : objects,
    'title' : city,
    'type' : type,
    }
return render_to_response('properties/properties_list.html', context_instance=RequestContext(request, context))

traceback

Environment:

Request Method: GET
Request URL: http://localhost:8000/properties/single/vacation_home/1/
Django Version: 1.1
Python Version: 2.6.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.humanize',
 'properties',
 'config',
 'sorl.thumbnail',
 'haystack',
 'south',
 'debug_toolbar']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/alvin/workspace/timeshare/properties/views.py" in property_single
  272.     return property_single_context(request, type, property)
File "/home/alvin/workspace/timeshare/properties/views.py" in property_single_context
  265.     return render_to_response('properties/single.html', context, context_instance=RequestContext(request))
File "/usr/local/lib/python2.6/dist-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in render_to_string
  103.         t = get_template(template_name)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in get_template
  82.     template = get_template_from_string(source, origin, template_name)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in get_template_from_string
  90.     return Template(source, origin, name)
File "/usr/local/lib/python2.6/dist-packages/django_debug_toolbar-0.8.1.alpha-py2.6.egg/debug_toolbar/panels/template.py" in new_template_init
  28.     old_template_init(self, template_string, origin, name)
File "/usr/local/lib/python2.6/dist-packages/django/template/__init__.py" in __init__
  168.         self.nodelist = compile_string(template_string, origin)
File "/usr/local/lib/python2.6/dist-packages/django/template/__init__.py" in compile_string
  189.     return parser.parse()
File "/usr/local/lib/python2.6/dist-packages/django/template/__init__.py" in parse
  285.                     compiled_result = compile_func(self, token)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in do_extends
  169.     nodelist = parser.parse()
File "/usr/local/lib/python2.6/dist-packages/django/template/__init__.py" in parse
  285.                     compiled_result = compile_func(self, token)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in do_block
  147.     nodelist = parser.parse(('endblock', 'endblock %s' % block_name))
File "/usr/local/lib/python2.6/dist-packages/django/template/__init__.py" in parse
  289.                 self.extend_nodelist(nodelist, compiled_result, token)
File "/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in extend_nodelist
  56.         node.source = token.source

Exception Type: AttributeError at /properties/single/vacation_home/1/
Exception Value: 'NoneType' object has no attribute 'source'

Any ideas for where to look in debugging this are welcomed - big huge thanks in advance if you happen to know what is causing the error

+1  A: 

Line 265 (or 272) of your source views.py is sending an object that is a 'NoneType' which deeper code is trying to get the 'source' attribute from. In other words, you're sending a blank (NoneType) object to the template engine, so look at your code above these lines and see which objects don't have a value when you send them away.

KevinDTimm
modified the template tag a bit and added the view for reference - line 265 is where the template context dict is defined - thing is that when I comment out the template tag everything works just fine... has me scratching my head...
Alvin
With respect to the Oracle generic stored proc question... there is absolutely *nothing* wrong with questions that can be googled. Google can give conflicting, incomplete, out-of-date, wrong or bad information. Jeff/Joel have repeatedly stated that "googleable" questions are fine so keep any comments on laziness to yourself. They're uncalled for.
cletus
+1  A: 

I've run into this a few times and it's always been the same thing (for me at least). If the template tag function doesn't return anything this error pops up.

class MyTag(template.Node):
  def __init__(self, name):
    self.name=name
  def render(self, context):
    context[self.name]='czarchaic'
    #return an empty string since we've only modified the context
    return ''
@register.tag
def my_tag(parser, token):
  bits=token.split_contents()
  if len(bits)==2:
    return MyTag(bits[1])

  #return an empty string if all test fail
  return ''

EDIT Looking at your code it would appear that if num were still 0 at the if num: check this tag would not return anything, resulting in this error.

czarchaic
Adding a string output in case of num = 0 got error: 'str' object has no attribute 'source' - commenting out all code and leaving return 'test' got the same error... programmed around this thing for now but bugging me that I can't figure it out... any other idead??
Alvin
Wow, a real stumper :). Could you post your template? Looking at your code again, I am curious about `tag_name, prop = token.split_contents()`. Is the second bit (prop) a string or template variable? I've always had to run template variables through a `template.Variable(prop).resolve(context)`.
czarchaic
Also, have you considered passing the bits off to a `template.Node` subclass, thereby giving you access to the context? This might be better suited as a simple_tag
czarchaic