Django html页面 'ascii' codec can't encode characters in position 8-10: ordinal not
用Django开发的页面,之前用的是python3.X,后来又换成python2.X后各种报错,编码问题,于是在所有python文件开头加了编码:#coding=utf-8
但是后来发现,有些文件加了#coding=utf-8还是不起作用,如现在在一个网页报错:
于是在Django项目的views.py下加了如下四行代码:
# coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
后来发现有网友和我的问题一样,但是他的解决方法和我不一样:https://stackoverflow.com/questions/27435622/python-ascii-codec-cant-encode-character-u-xe9-in-position-5-ordinal-not
我也贡献了我的答案my solution:
Hi,your problem is same with mine,just encode question, you can add bellow code in your views.py of django project:
# coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
my problem:
Error during template rendering
In template D:\PythonProjects\DjangoProject\guest\sign\templates\sign\guest_manage.html, error at line 72
I'm trying to run a quick Django application that pulls data from Google AdWords and exposes the names of accounts that are managed by an agency. When doing so, I get the following error:
Here's the snippet:
where the call to I have already added
to the template I am rendering, but it still fails, so I believe the problem is not on the HTML template, but rather on the Python/Django engine. Any ideas how I can fix it? Here's the View code that renders the template:
UPDATED Question
What's also curious is that if I remove this:
and I just print out the main array:
it works just fine. Not sure what's going on. Curious fact #2: As I managed to output the full array, I checked for character 'é' and I didn't find it on the final output. Not sure where it was coming from. |
||||
The problem is likely that, in some place in your code, you accidentally defined a data structure to be a Python byte string, when you should have made it a Python Unicode string. This leads Django and Python to convert from Django's Unicode string to your byte string, in a default way, using the ASCII codec. The error message gives some clues:
Function render_to_response() uses settings DEFAULT_CHARSET and DEFAULT_CONTENT_TYPE](https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DEFAULT_CONTENT_TYPE). They should default to 'utf-8' and 'text/html', which should be appropriate for generating a UTF-8 encoded HTML page, but check. I like the suggestion that you check your Character handling, and Unicode vs byte strings, are handled differently in Python 2 and Python 3. Which version of Python are you using? Update: Python 2.7.6, thanks. The Unicode HOWTO I linked to above is for Python 2.7.x. If you make sure your code handles strings as Unicode throughout, unless you really want a byte string, that will likely fix this problem. Update: Consider modifying your template to give you debugging information. Try something like these expressions, to see what's really in
[Updated in response to multiple updates from original poster.] |
|||||
|
本文来自博客园,作者:热爱技术的小牛,转载请注明原文链接:https://www.cnblogs.com/my-blogs-for-everone/p/8295260.html
__unicode__
method in yourmodels.py
would work – dazedconfused Dec 12 '14 at 1:57models.py
? – dazedconfused Dec 12 '14 at 2:13managed_accounts
– dazedconfused Dec 12 '14 at 2:18