我在嘗試生成事件模型報告時遇到此錯誤,此行爲{{ event.reports_dump | safe }}
tmpls.append("<span>"+event.human_date_range()+"</span>")
它使用創建的自定義函數來創建可讀文本。Django無法連接'str'和'NoneType'對象
我試圖通過第一返回字符串「無」來解決問題,而不是什麼
def human_date_range(self):
...
if not (starts and ends):
#from return to return "None"
return "None"
但遷移和重新啓動服務器後持續存在的錯誤。我可以告訴的變化已經生效從錯誤日誌,所以我overrided全功能,但它仍然返回相同的錯誤
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
請嘗試只包含*相關*部分的代碼。你所包含的函數只返回一個字符串,並忽略其餘部分,所以我不能看到問題的實際位置。你可能想嘗試設置一個斷點,並通過 – Sayse
你怎麼期待'human_date_range'函數返回除了字符串「None」之外的任何東西 - 在聲明'starts'和'ends'後立即調用'return'? – Brandon