Unicode in Django unit test
我尝试在Django单元测试中使用utf8字符串并包含
1 | # -*- coding: utf-8 -*- |
但django-admin.py仍抱怨没有编码。
Traceback (most recent call last):
File"/home/basti/work/virtualenv/bin/django-admin.py", line 5, in
management.execute_from_command_line()
File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/init.py",
line 429, in execute_from_command_line
utility.execute()File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/init.py",
line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)File
"> /home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/base.py",
line 191, in run_from_argv
self.execute(*args, **options.dict)File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/base.py",
line 220, in execute
output = self.handle(*args, **options)File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/south/management/commands/test.py",
line 8, in handle
super(Command, self).handle(*args, **kwargs)File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/django/core/management/commands/test.py",
line 37, in handle
failures = test_runner.run_tests(test_labels)File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py",
line 358, in run_tests
suite = self.build_suite(test_labels, extra_tests)File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py",
line 248, in build_suite
suite.addTest(build_suite(app))File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py",
line 77, in build_suite
test_module = get_tests(app_module)File
"/home/basti/work/virtualenv/lib/python2.7/site-packages/django/test/simple.py",
line 35, in get_tests
test_module = import('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)SyntaxError: Non-ASCII character '\xc3' in file tests.py on line 242,
but no encoding declared; see http://www.python.org/peps/pep-0263.html
for details
代码是
# - 编码:utf-8 -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | """ This file demonstrates writing tests using the unittest module. These will pass when you run"manage.py test". Replace this with more appropriate tests for your application. """ from django.test import TestCase, Client from django.contrib.auth.models import User from django.db.utils import IntegrityError from django.core.urlresolvers import reverse from django.utils.encoding import smart_unicode class ViewTests(TestCase): def setUp(self): self.client = Client() def test_french(self): self.client.cookies["django_language"] = 'fr' r = self.client.get("/") self.assertTrue(smart_unicode(u"Se déconnecter") in r.content) |
我试图将TEST_CHARSET和TEST_DATABASE_CHARSET设置为utf8,但仍然没有运气。
关于如何解决这个问题的任何提示?
TIA&& 祝你今天愉快!
巴斯蒂
放:
1 | # -*- coding: utf-8 -*- |
作为该文件的第一行。
要回答我自己的问题,问题不是匹配的字符串,而是请求内容的编码!
随后更改测试修复它
self.assertTrue(你在r.content.decode('utf8')中的"sedéconnecter")
我有这个问题,因为我的models.py编码没有为文件设置。将
确保您在每个文件中定义了编码,包括模型,当然还有tests.py。如果可能,请获取最新版本的django。
如果管理员仍然无法正常工作,请尝试:
解决方案#1:
1 | from django.utils.encoding import smart_unicode |
在模型定义上:
1 2 | def __unicode__(self): return smart_unicode(("%s" % self.field)) |
解决方案#2:文字方法
1 2 | def __unicode__(self): return (u"%s" % self.field) |
始终设定
1 | DEFAULT_CHARSET='utf-8' |
在settings.py中
你也可以翻译[这篇文章] [1]
[1]:来自西班牙语的http://www.pvilas.com/2011/03/django-evitar-error-en-el-administrador.html。我希望可以提供帮助。