我有课:
class Training_unit(models.Model):
name = models.CharField(max_length = 200)
description = models.CharField(max_length = 200)
我想现在返回我尝试这两种方法的名称和描述:
def __str__(self):
return force_bytes('%s %s' % (self.name, self.description))
和
def __str__(self):
return '%s %s' % (self.name, self.description)
我在
上找到了这些功能https://docs.djangoproject.com/en/1.8/ref/models/instances/#django.db.models.Model.str
但两者都在我的管理页面上显示了这个错误:
修改 完整回溯消息:
Environment:
Request Method: GET
Request URL: http://localhost:8000/admin/trainings/training_block/3/
Django Version: 1.8.2
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'trainings')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Template error:
In template C:\Python34\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 19
__str__ returned non-string (type datetime.date)
9 : {% for field in line %}
10 : <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
11 : {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
12 : {% if field.is_checkbox %}
13 : {{ field.field }}{{ field.label_tag }}
14 : {% else %}
15 : {{ field.label_tag }}
16 : {% if field.is_readonly %}
17 : <p>{{ field.contents }}</p>
18 : {% else %}
19 : {{ field.field }}
20 : {% endif %}
21 : {% endif %}
22 : {% if field.field.help_text %}
23 : <p class="help">{{ field.field.help_text|safe }}</p>
24 : {% endif %}
25 : </div>
26 : {% endfor %}
27 : </div>
28 : {% endfor %}
29 : </fieldset>
Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
164. response = response.render()
File "C:\Python34\lib\site-packages\django\template\response.py" in render
158. self.content = self.rendered_content
File "C:\Python34\lib\site-packages\django\template\response.py" in rendered_content
135. content = template.render(context, self._request)
File "C:\Python34\lib\site-packages\django\template\backends\django.py" in render
74. return self.template.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
209. return self._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in _render
201. return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\loader_tags.py" in render
135. return compiled_parent._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in _render
201. return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\loader_tags.py" in render
135. return compiled_parent._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in _render
201. return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\loader_tags.py" in render
65. result = block.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\loader_tags.py" in render
65. result = block.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Python34\lib\site-packages\django\template\loader_tags.py" in render
159. return template.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
211. return self._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in _render
201. return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Python34\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Python34\lib\site-packages\django\template\defaulttags.py" in render
329. return nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\defaulttags.py" in render
329. return nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render
92. output = force_text(output)
File "C:\Python34\lib\site-packages\django\utils\encoding.py" in force_text
90. s = six.text_type(s)
File "C:\Python34\lib\site-packages\django\utils\html.py" in <lambda>
399. klass.__str__ = lambda self: mark_safe(klass_str(self))
File "C:\Python34\lib\site-packages\django\forms\forms.py" in __str__
543. return self.as_widget()
File "C:\Python34\lib\site-packages\django\forms\forms.py" in as_widget
599. return force_text(widget.render(name, self.value(), attrs=attrs))
File "C:\Python34\lib\site-packages\django\contrib\admin\widgets.py" in render
292. 'widget': self.widget.render(name, value, *args, **kwargs),
File "C:\Python34\lib\site-packages\django\forms\widgets.py" in render
513. options = self.render_options(choices, [value])
File "C:\Python34\lib\site-packages\django\forms\widgets.py" in render_options
539. for option_value, option_label in chain(self.choices, choices):
File "C:\Python34\lib\site-packages\django\forms\models.py" in __iter__
1103. yield self.choice(obj)
File "C:\Python34\lib\site-packages\django\forms\models.py" in choice
1110. return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
File "C:\Python34\lib\site-packages\django\forms\models.py" in label_from_instance
1181. return smart_text(obj)
File "C:\Python34\lib\site-packages\django\utils\encoding.py" in smart_text
56. return force_text(s, encoding, strings_only, errors)
File "C:\Python34\lib\site-packages\django\utils\encoding.py" in force_text
90. s = six.text_type(s)
Exception Type: TypeError at /admin/trainings/training_block/3/
Exception Value: __str__ returned non-string (type datetime.date)
答案 0 :(得分:3)
你正在寻找错误的地方。这个模型__str__
没问题。
尝试点击
File "C:\Python34\lib\site-packages\django\forms\forms.py" in as_widget
599. return force_text(widget.render(name, self.value(), attrs=attrs))
您将在此行看到本地变量,而name
应指向实际有问题的字段。
答案 1 :(得分:2)
这似乎是管理界面中使用的窗口小部件中的问题,而不是直接在您的Training_unit中。 检查您的Trainint_unit管理员模型,可能存在问题。也许您为管理模型使用TabularInline,以便加载其他东西(如Date小部件)?