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.] |
|||||
|
Your Answer
Django html页面 'ascii' codec can't encode characters in position 8-10: ordinal not的更多相关文章
- 解决UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position
最近用Python写了些爬虫,在爬取一个gb2312的页面时,抛出异常: UnicodeEncodeError: 'ascii' codec can't encode characters in po ...
- Python编码问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(12
今天安装了PyScripter编辑器,刚要写代码,突然就出现异常: <span style="font-size:14px;color:#ff0000;">>&g ...
- python+selenium运行报错UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
使用python+selenium运行自动化脚本时,打印某一段文字出现UnicodeEncodeError: 'ascii' codec can't encode characters in posi ...
- 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)的解决办法
使用的python2.7,运行的时候出现了'ascii' codec can't encode characters in position 0-8: ordinal not in range(128 ...
- Pip 安装 出现UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in
在Python 环境下,使用PiP 命令安装时,报错提示: UnicodeEncodeError: 'ascii' codec can't encode characters in position ...
- 解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 问题(转)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-11: ordinal not in range(128 ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-15: ordinal not in range(128)
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报类似这样的错误. UnicodeEncodeError: 'ascii' codec can't ...
- 解决Python2.7的UnicodeEncodeError:'ascii' codec can't encode characters in position 0-78: ordinal not in range(128)异常错误
解决Python2.7的UnicodeEncodeError: 'ascii' codec can't encode异常错误 大家都知道,在使用python进行网络爬虫时,最头疼的就是转码问题,下面是 ...
- 【转】Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1
转自:https://blog.csdn.net/AckClinkz/article/details/78538462 环境 >>> import sys >>> ...
随机推荐
- 怎么选择最适合自己的Linux培训机构?
Linux培训已经成为入门Linux的一个重要途径,它的优势在于学习知识的系统性.快速性和实用性.Linux培训毕业的学员大多数拥有较强的实战动手能力,能够较快上手,更符合企业需求. 不过,大部分同学 ...
- kernel-常见参数或宏
kernel-常见参数或宏 get_online_cpus get_online_cpus(); get_online_mems(); kstrdup_const 分配内存 cache_name = ...
- STM32_NVIC寄存器详解
在MDK内,与NVIC相关的寄存器,MDK为其定义了如下的结构体: typedef struct { vu32 ISER[2]; //2个32位中断使能寄存器分别对应到60 ...
- Maven学习总结(2)——Maven项目构建过程练习
Maven学习总结(二)--Maven项目构建过程练习 上一篇只是简单介绍了一下maven入门的一些相关知识,这一篇主要是体验一下Maven高度自动化构建项目的过程 一.创建Maven项目 1.1.建 ...
- 利用fontforge制作自己的字体
最近手伤了,写代码特别慢,索性就干干一些奇奇怪怪的事情. 发现我电脑上的中文字体很是奇怪,于是便去找了中英混合的等宽字体. 满足条件的只找到了YaHei Consolas Hybrid,是微软的Con ...
- python在Linux中安装虚拟环境,区别python2和python3,分别安装模块
安装虚拟环境的时候遇到的问题,解决的过程很闹心,在这里简单直接的分享出来,就是为了解决问题. 安装虚拟环境(须在联网状态下) $ sudo pip install virtualenv $ sudo ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- spring boot开发REST接口
1.配置pom.xml文件的<parent>和<depencencies>,指定spring boot web依赖 <parent> <groupId> ...
- cocoa 运行apple脚本文件的方法
NSString* path = [[NSBundle mainBundle] pathForResource:@"ScriptName" ofType:@"scpt&q ...
- S - Best Reward 扩展KMP
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...