gpt4 book ai didi

Django buggy 模板标签 - 'NoneType' 对象没有属性 'source'

转载 作者:行者123 更新时间:2023-12-05 00:42:38 25 4
gpt4 key购买 nike

想知道是什么原因造成的?让我难住了一段时间,当分段运行时,所有内容都会在控制台中检查出来

作为旁注:
模板在其他地方使用相同的对象并显示值 - 模板中的对象也与下面控制台中加载的对象相同

错误

'NoneType' 对象没有属性 'source'

模板

{% form_transaction prop %}

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'

欢迎任何有关在哪里查看调试的想法 - 如果您碰巧知道导致错误的原因,请提前致谢

最佳答案

我已经遇到过几次,而且总是一样的事情(至少对我而言)。如果模板标记函数没有返回任何内容,则会弹出此错误。

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 ''

编辑
看看你的代码,如果 numif num: 处仍为 0检查此标记不会返回任何内容,从而导致此错误。

关于Django buggy 模板标签 - 'NoneType' 对象没有属性 'source',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1879614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com