0

将 django startapp 命令与自定义应用程序模板一起使用时出现一个奇怪的错误。我创建了一个自定义应用程序模板,在那里我有一个models.py带有 unicode 字符的文件,如下所示:

# -*- coding: utf-8 -*-
from django.db import models
class {{app_name|capfirst}}(models.Model):
    """Toto je text dokumentace. Žluťoučký kůň"""
    pass

当我运行文件时没有得到处理,我得到这个错误python manage.py startapp --template=core/my_app_template applicationmodels.py

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 459, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/startapp.py", line 25, in handle
    super(Command, self).handle('app', app_name, target, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/templates.py", line 162, in handle
    new_file.write(content)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u017d' in position 112: ordinal not in range(128)

如何对文件进行编码以便对其进行处理?我想# -*- coding: utf-8 -*-就够了。或者我应该在里面设置settings.py什么?

我查看了代码,在将内容写入文件时抛出了错误:

with open(new_path, 'w') as new_file:
    new_file.write(content)

所以我怀疑这是 django 的错。

4

2 回答 2

1
with open(new_path, 'w') as new_file:
    new_file.write(content).encode('utf-8')
于 2013-02-28T15:06:58.583 回答
0

我使用了 Django 1.4。Django 1.5 解决了这个问题。他们更新了代码并完全按照凯瑟琳的建议做了。

于 2013-03-02T14:07:59.313 回答