想知道造成这种情况的原因是什么?如果我被困了一段时间,那么一切都会在控制台中检查出来
作为附注: 模板在其他地方使用相同的对象并显示值 - 模板中的对象也与下面控制台中加载的对象相同
'NoneType'对象没有属性'source'
{% form_transaction 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>'
TRANSACTION_TITLE = (
(1, 'Purchase'),
(2, 'Rental'),
(3, 'Exchange'),
)
TRANSACTION_MODEL = ['sell', 'rent', 'exchange']
@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]
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))
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'
欢迎任何关于在哪里查看调试的想法 - 如果您碰巧知道导致错误的原因,请提前非常感谢
答案 0 :(得分:4)
我已经遇到过这几次,它总是一样的(至少对我而言)。如果模板标记功能未返回任何内容,则会弹出此错误。
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
查看代码时,如果num
检查if num:
仍为0,则此标记不会返回任何内容,从而导致此错误。
答案 1 :(得分:2)
你的源views.py的第265行(或第272行)正在发送一个'NoneType'对象,深层代码试图从中获取'source'属性。换句话说,您正在向模板引擎发送一个空白(NoneType)对象,因此请查看这些行上方的代码,并在发送它们时查看哪些对象没有值。