django后台显示出了数据表,但是添加或者修改时报错TypeError at /admin/* str returned non-string (type int):
这是因为在你的models.py文件里, def unicode(self) 或 def str(self) 返回的不是字符串格式,例如:
def __str__(self):
# 在Python3中使用 def __str__(self),Python2中使用def __unicode__(self)
return self.age
这里age是int型的,会报错,改成:
def __str__(self):
# 在Python3中使用 def __str__(self),Python2中使用def __unicode__(self)
return str(self.age)
这样就可以了!其实报错里已经说得非常详细了,str returned non-string (type int):str 返回的不是字符串。如果你遇到同样的问题,试试看吧。
补充完整报错内容:
TypeError at /admin/app1/article/add/
__str__ returned non-string (type int)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/app1/article/add/
Django Version: 2.1.4
Exception Type: TypeError
Exception Value:
__str__ returned non-string (type int)
Exception Location: C:\Users\Administrator\PycharmProjects\untitled1\venv\lib\site-packages\django\forms\models.py in label_from_instance, line 1218
Python Executable: C:\Users\Administrator\PycharmProjects\untitled1\venv\Scripts\python.exe
Python Version: 3.7.2
Python Path:
['C:\\Users\\Administrator\\PycharmProjects\\untitled1',
'C:\\Users\\Administrator\\PycharmProjects\\untitled1',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\Administrator\\PycharmProjects\\untitled1\\venv',
'C:\\Users\\Administrator\\PycharmProjects\\untitled1\\venv\\lib\\site-packages',
'C:\\Users\\Administrator\\PycharmProjects\\untitled1\\venv\\lib\\site-packages\\setuptools-39.1.0-py3.7.egg',
'C:\\Users\\Administrator\\PycharmProjects\\untitled1\\venv\\lib\\site-packages\\pip-10.0.1-py3.7.egg',
'C:\\Program Files\\JetBrains\\PyCharm '
'2018.3.2\\helpers\\pycharm_matplotlib_backend']
Server time: Sat, 12 Jan 2019 01:24:18 +0000
Error during template rendering
In template C:\Users\Administrator\PycharmProjects\untitled1\venv\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 19
__str__ returned non-string (type int)