输出中的错误日志:NameError at /admin/todo/todomodel/add/name 'unicode' is not definedRequest Method: POSTRequest URL: http://127.0.0.1:8000/admin/todo/todomodel/add/Django Version: 2.2Exception Type: NameErrorException Value: name 'unicode' is not defined关于系统和异常的更多细节:异常位置:C:\Users\PEE TEE\Desktop\ToDoApp\lib\site-packages\slugify.py in slugify, line 24Python可执行文件: C:\Users\PEE TEE\Desktop\ToDoApp\Scripts\python.exe蟒蛇版本:3.7.6蟒蛇路径:['C:\\Users\\PEE TEE\\Desktop\\ToDoApp\\src', 'C:\\Users\\PEE TEE\\Desktop\\ToDoApp\\Scripts\\python37.zip', 'C:\\Users\\PEE TEE\\Desktop\\ToDoApp\\DLLs', 'C:\\Users\\PEE TEE\\Desktop\\ToDoApp\\lib', 'C:\\Users\\PEE TEE\\Desktop\\ToDoApp\\Scripts', 'c:\\users\\pee tee\\appdata\\local\\programs\\python\\python37\\Lib', 'c:\\users\\pee tee\\appdata\\local\\programs\\python\\python37\\DLLs', 'C:\\Users\\PEE TEE\\Desktop\\ToDoApp', 'C:\\Users\\PEE TEE\\Desktop\\ToDoApp\\lib\\site-packages']服务器时间:2020 年 2 月 4 日星期二 03:03:37 +0000这是我的模型:from django.db import modelsfrom django.db.models.signals import post_savefrom slugify import slugify# Create your models here.class ToDoModel(models.Model): author = models.ForeignKey('auth.User', on_delete=models.CASCADE) what_todo = models.CharField(max_length=255, default="Event to be done") date_todo = models.DateField(auto_now=False,auto_now_add=False) slug = models.SlugField(null=True, blank=True) def save(self, *args, **kwargs): if self.what_todo and not self.slug: self.slug = slugify(self.what_todo) return super(ToDoModel, self).save(*args, **kwargs) def __str__(self): return str(self.what_todo)
2 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
您正在使用一些与 Python 2 相关的代码,Python 3 将unicode
类型重命名为str
. 请参阅https://docs.python.org/3/howto/unicode.html中的详细信息
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
更正你的 slugify 代码
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.what_todo)
return super(ToDoModel, self).save(*args, **kwargs)
添加回答
举报
0/150
提交
取消