猿问

NameError 'unicode' not defined,当我在 django 模型中使用保存

输出中的错误日志:


NameError at /admin/todo/todomodel/add/

name 'unicode' is not defined

Request Method: POST

Request URL:    http://127.0.0.1:8000/admin/todo/todomodel/add/

Django Version: 2.2

Exception Type: NameError

Exception Value:    

name 'unicode' is not defined

关于系统和异常的更多细节:


异常位置:C:\Users\PEE TEE\Desktop\ToDoApp\lib\site-packages\slugify.py in slugify, line 24


Python可执行文件: 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 models

from django.db.models.signals import post_save

from 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)


小唯快跑啊
浏览 135回答 2
2回答

缥缈止盈

您正在使用一些与 Python 2 相关的代码,Python 3 将unicode类型重命名为str. 请参阅https://docs.python.org/3/howto/unicode.html中的详细信息

摇曳的蔷薇

更正你的 slugify 代码def save(self, *args, **kwargs):    if not self.slug:        self.slug = slugify(self.what_todo)    return super(ToDoModel, self).save(*args, **kwargs)
随时随地看视频慕课网APP

相关分类

Python
我要回答