Reputation: 17934
I am using Django 1.7.3 on a Mac.
I have created a Django project in the following path:
/Users/cheng/百度云同步盘/Dev/django/testproject/
(The project is called 'testproject')
I am having trouble loading a template file:
/Users/cheng/百度云同步盘/Dev/django/testproject/
-> vis
-> templates
-> vis
-> index.html
(my app name is called 'vis')
When I hit the right URL, I got:
UnicodeEncodeError at /vis/
'ascii' codec can't encode characters in position 13-18: ordinal not in range(128)
Python Path: ['/Users/cheng/\xe7\x99\xbe\xe5\xba\xa6\xe4\xba\x91\xe5\x90\x8c\xe6\xad\xa5\xe7\x9b\x98/Dev/django/testproject' ...
Unicode error hint
The string that could not be encoded/decoded was: heng/百度云同步盘/Dev/
As you can see, the unicode portion of the path '百度云同步盘' has been ascii encoded as '\xe7\x99\xbe\xe5\xba\xa6\xe4\xba\x91\xe5\x90\x8c\xe6\xad\xa5\xe7\x9b\x98'.
Is there anyway to solve this problem besides moving the project to a non-unicode dir?
Thank you!
Update: I am using python 2.7.9. Full stack trace:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/vis/
Django Version: 1.7.3
Python Version: 2.7.9
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'vis')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/cheng/百度云同步盘/Dev/django/testproject/vis/views.py" in index
7. return render(request, 'vis/index.html', context)
File "/usr/local/lib/python2.7/site-packages/django/shortcuts.py" in render
50. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
170. t = get_template(template_name, dirs)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
144. template, origin = find_template(template_name, dirs)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
126. loader = find_template_loader(loader_name)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in find_template_loader
98. TemplateLoader = import_string(loader)
File "/usr/local/lib/python2.7/site-packages/django/utils/module_loading.py" in import_string
26. module = import_module(module_path)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/usr/local/lib/python2.7/site-packages/django/template/loaders/app_directories.py" in <module>
33. app_template_dirs = calculate_app_template_dirs()
File "/usr/local/lib/python2.7/site-packages/django/template/loaders/app_directories.py" in calculate_app_template_dirs
27. template_dir = template_dir.decode(fs_encoding)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py" in decode
16. return codecs.utf_8_decode(input, errors, True)
Exception Type: UnicodeEncodeError at /vis/
Exception Value: 'ascii' codec can't encode characters in position 13-18: ordinal not in range(128)
Upvotes: 1
Views: 813
Reputation: 536529
File "/usr/local/lib/python2.7/site-packages/django/template/loaders/app_directories.py" in calculate_app_template_dirs
27. template_dir = template_dir.decode(fs_encoding)
This appears to be a bug in the Django template loader.
It is trying to .decode
a string that it already Unicode, causing an implicit .encode
to the default encoding which in your case is ASCII so it can't encode the Chinese. The string in question is a module file path, which comes from AppConfig.path, which is defined to be a Unicode string.
I suggest filing a bug against Django (eg ‘Template loader fails for path unencodable in default encoding’). In the meantime you could try to work around it by setting the default encoding to utf-8
in your sitecustomize.py
, or just run your app from a directory whose path is all-ASCII.
Upvotes: 1