我想用Python 2.7和Django 1.7.8构建一个博客。
当我使用Django时,我不断收到错误: UnicodeDecodeError
。
相关代码是:
#coding: utf-8
from django.db import models
class Entry(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
publish = models.BooleanField(default=False)
./manage.py makemigrations
收到错误:
Migrations for 'blog':
0001_initial.py:
- Create model Entry
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7.8-py2.7.egg/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/Django-1.7.8-py2.7.egg/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7.8-py2.7.egg/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7.8-py2.7.egg/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7.8-py2.7.egg/django/core/management/commands/makemigrations.py", line 124, in handle
self.write_migration_files(changes)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7.8-py2.7.egg/django/core/management/commands/makemigrations.py", line 143, in write_migration_files
migrations_directory = os.path.dirname(writer.path)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7.8-py2.7.egg/django/db/migrations/writer.py", line 222, in path
return os.path.join(basedir, self.filename)
File "/usr/lib/python2.7/posixpath.py", line 80, in join
path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 10: ordinal not in range(128)
我不知道出了什么问题,任何想法?我已在代码顶部添加 coding:utf-8
答案 0 :(得分:1)
你已经在模型中有数据吗? 如果是这样,我猜你在那里有一些不好的角色。 您是否在模型上定义了 unicode 或 str 方法? 如果是这样,我建议在这样的尝试声明中使用它们:
def __str__():
try:
return "%s" % self.title
except:
return "%s" % self.pk
当您只看到PK时,您会知道您的“标题”字段包含错误的数据。 将其扩展为包含您想要显示的字段,而不仅仅是标题。
答案 1 :(得分:1)
加入路径时看起来会失败吗?
也许您的文件夹名称无法转换为ascii?
另外,考虑编辑出错的文件?也许在失败的行之前添加一个print语句,显示正在尝试加入的内容?
编辑下面提到的文件,添加print语句以查看尝试加入的内容?
File "/usr/lib/python2.7/posixpath.py", line 80, in join
path += '/' + b