Je reçois une erreur étrange lors de l'utilisation de la commande django startapp avec un modèle d'application personnalisé. Je créé un modèle d'application personnalisée et là j'avoir un fichier models.py
avec des caractères unicode comme ceci:Django startapp avec le modèle personnalisé unicodeerror
# -*- coding: utf-8 -*-
from django.db import models
class {{app_name|capfirst}}(models.Model):
"""Toto je text dokumentace. Žluťoučký kůň"""
pass
Quand je lance python manage.py startapp --template=core/my_app_template application
le fichier models.py
ne soit pas proccessed et je reçois cette erreur:
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)
Comment puis-je encoder le fichier pour qu'il soit traité? Je pensais que # -*- coding: utf-8 -*-
est suffisant. Ou y at-il quelque chose que je devrais définir dans le settings.py
?
Je regardais le code et l'erreur obtient Renvoyé lorsque l'écriture du fichier:
with open(new_path, 'w') as new_file:
new_file.write(content)
Je doute que c'est la faute d'un django.
Si vous utilisez le codage '# - *: utf-8 - * -', vous devriez enregistrer le fichier codé en UTF-8. – Matthias
@Matthias Il est sauvegardé avec l'encodage UTF-8. – davekr