我尝试为{{ event.reports_dump | safe }}
来自此行tmpls.append("<span>"+event.human_date_range()+"</span>")
生成事件模型的报告时遇到此错误,它使用创建的自定义函数来制作人类可读文本。
我尝试通过首先返回字符串&#34;无&#34;来解决问题。而不是什么
def human_date_range(self):
...
if not (starts and ends):
#from return to return "None"
return "None"
但迁移并重新启动服务器后错误仍然存在。我可以告诉更改已从错误日志生效,所以我覆盖了整个函数,但它仍然返回相同的错误
def human_date_range(self):
...
return "None"
if not (starts and ends):
#from return to return "None"
return "None"
需要一些调试帮助。
我已经粘贴了错误日志和模型
class Event(BaseItemModel, AdminURLMixin):
venue = models.ForeignKey(Venue)
categories = TreeManyToManyField(Category)
parent = models.ForeignKey('Event', blank=True, verbose_name='Parent Event', null=True,)
contact_person = models.CharField(max_length=255, blank=True,)
contact_email = models.EmailField(max_length=255, blank=True, help_text='Only one valid email address allowed')
contact_phone = models.CharField(max_length=255, blank=True,)
contact_website = models.URLField(max_length=255, blank=True, help_text='Only one valid web address allowed. Must include http:// or https://')
contact_address = models.TextField(blank=True,)
free = models.BooleanField(default=False)
taxi = models.NullBooleanField(default=None)
jkia_ad = models.NullBooleanField(default=None)
cost = models.PositiveIntegerField(blank=True, null=True, verbose_name="Cost in KES", default=0, help_text='Only numbers allowed, if price is not in KES convert the amount to KES and write the Currency and Cost in Cost Description')
cost_desc = models.TextField(blank=True, verbose_name='Cost description', help_text='If several prices available write them here using the regular price in Cost')
artist = models.TextField(blank=True,)
event_ticket = models.IntegerField(blank=True, null=True)
num_days = models.IntegerField(default=0, editable=False, db_index=True,)
old_dates = models.TextField(blank=True, editable=False,)
def human_date_range(self):
starts = self.starts_datetime()
ends = self.ends_datetime()
return "None"
if not (starts and ends):
return "None"
if starts.date() == ends.date():
# February 21st, 10pm
return starts.strftime('%B ')+humanize.ordinal(int(starts.strftime('%d')))+', '+str(int(starts.strftime('%I')))+starts.strftime('%p').lower()
elif starts.year == ends.year and starts.month == ends.month:
# February 21st, 10pm-11pm
return\
starts.strftime('%B ')+humanize.ordinal(int(starts.strftime('%d')))+', '+\
str(int(starts.strftime('%I')))+starts.strftime('%p').lower()+' - '+str(int(ends.strftime('%I')))+ends.strftime('%p').lower()
elif starts.year == ends.year:
# February 21st - March 23rd, 10pm-11pm
return\
starts.strftime('%B ')+humanize.ordinal(int(starts.strftime('%d')))+' - '+ends.strftime('%B ')+humanize.ordinal(int(ends.strftime('%d')))+', '+\
str(int(starts.strftime('%I')))+starts.strftime('%p').lower()+' - '+str(int(ends.strftime('%I')))+ends.strftime('%p').lower()
else:
# February 21st 2013 - March 4th 2014, 10pm-11pm
return starts.strftime('%B ')+humanize.ordinal(int(starts.strftime('%d')))+starts.strftime(' %Y')+' - '+ends.strftime('%B ')+humanize.ordinal(int(ends.strftime('%d')))+ends.strftime(' %Y')+', '+\
str(int(starts.strftime('%I')))+starts.strftime('%p').lower()+' - '+str(int(ends.strftime('%I')))+ends.strftime('%p').lower()
错误日志
Environment:
Request Method: GET
Request URL: http://stage.kenyabuzz.com/admin/reports/events/2015-12-8-0-0/2015-12-14-0-0/
Django Version: 1.7.10
Python Version: 2.7.5
Template error:
In template /home/kbuzz/webapps/kenyabuzz/kb/reports/templates/reports/report.html, error at line 71
cannot concatenate 'str' and 'NoneType' objects
61 :
62 : <div>
63 : {% for event in stat.v %}
64 :
65 : <div class='event'>
66 :
67 : {% if event.title %}
68 : <span class='bold'>{{ event.title }}: </span>
69 : {% endif %}
70 :
71 : {{ event.reports_dump | safe }}
72 :
73 : {% if event.description %}
74 : <span>{{ event.description }}</span>
75 : {% endif %}
76 :
77 : </div>
78 :
79 : {% endfor %}
80 :
81 : </div>
Traceback:
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/contrib/auth/decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/home/kbuzz/webapps/kenyabuzz/kb/reports/views.py" in events
205. RequestContext(request, {})
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/shortcuts.py" in render_to_response
25. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/loader.py" in render_to_string
178. return t.render(context_instance)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in render
148. return self._render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in _render
142. return self.nodelist.render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/debug.py" in render_node
80. return node.render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/loader_tags.py" in render
126. return compiled_parent._render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in _render
142. return self.nodelist.render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/debug.py" in render_node
80. return node.render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/loader_tags.py" in render
126. return compiled_parent._render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in _render
142. return self.nodelist.render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/debug.py" in render_node
80. return node.render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/loader_tags.py" in render
65. result = block.nodelist.render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/debug.py" in render_node
80. return node.render(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/defaulttags.py" in render
201. nodelist.append(node.render(context))
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/defaulttags.py" in render
201. nodelist.append(node.render(context))
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/debug.py" in render
90. output = self.filter_expression.resolve(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in resolve
596. obj = self.var.resolve(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in resolve
734. value = self._resolve_lookup(context)
File "/home/kbuzz/webapps/kenyabuzz/lib/python2.7/Django-1.7.10-py2.7.egg/django/template/base.py" in _resolve_lookup
788. current = current()
File "/home/kbuzz/webapps/kenyabuzz/kb/events/models.py" in reports_dump
175. tmpls.append("<span>"+event.human_date_range()+"</span>")
Exception Type: TypeError at /admin/reports/events/2015-12-8-0-0/2015-12-14-0-0/
Exception Value: cannot concatenate 'str' and 'NoneType' objects
答案 0 :(得分:0)
你最好使用字符串插值,它可以处理这样的事情。
tmpls.append("<span>{}</span>".format(event.human_date_range()))