2013-02-28 3 views
0

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.

+0

Si vous utilisez le codage '# - *: utf-8 - * -', vous devriez enregistrer le fichier codé en UTF-8. – Matthias

+0

@Matthias Il est sauvegardé avec l'encodage UTF-8. – davekr

Répondre

0

J'ai utilisé Django 1.4. Ce problème est résolu avec Django 1.5. Ils ont mis à jour le code et ont fait exactement ce que catherine a suggéré.

1
with open(new_path, 'w') as new_file: 
    new_file.write(content).encode('utf-8') 
+0

C'est bon, mais je pense que je ne peux pas le faire sans forcer django ou en héritant de la classe TemplateCommand et en créant ma commande personnalisée. – davekr

+0

J'ai essayé de le fourchonner. Il sort l'attribut 'AttributeError:' NoneType 'n'a aucun attribut' encoder ' – davekr

+0

try new_file.write (content.encode (' utf-8 ')) – catherine

Questions connexes