Django:/accounts/ProfileDetails/'utf8' 处的 UnicodeDecodeError 编解码器无法解码位置 5915 中的字节 0xe1:无效的继续字节

Posted

技术标签:

【中文标题】Django:/accounts/ProfileDetails/\'utf8\' 处的 UnicodeDecodeError 编解码器无法解码位置 5915 中的字节 0xe1:无效的继续字节【英文标题】:Django : UnicodeDecodeError at /accounts/ProfileDetails/ 'utf8' codec can't decode byte 0xe1 in position 5915: invalid continuation byteDjango:/accounts/ProfileDetails/'utf8' 处的 UnicodeDecodeError 编解码器无法解码位置 5915 中的字节 0xe1:无效的继续字节 【发布时间】:2013-04-04 15:21:52 【问题描述】:

html 页面中添加 javascript 代码时出现此错误

UnicodeDecodeError at /accounts/ProfileDetails/

'utf8' 编解码器无法解码位置 5915 中的字节 0xe1:无效的继续字节

有人出主意

Traceback:

% 扩展“base.html”% get_response 中的文件“/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py” 111. 响应 = 回调(请求,*callback_args,**callback_kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
20.                 return view_func(request, *args, **kwargs)

File "/var/www/userreg/registration/views.py" in newpost
275.             return render_to_response('registration/personal_information.html',context_instance=RequestContext(request))

File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py" in render_to_response
20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
169.         t = get_template(template_name)

File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in get_template
145.     template, origin = find_template(template_name)

File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in find_template
134.             source, display_name = loader(name, dirs)

File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in __call__
42.         return self.load_template(template_name, template_dirs)

File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in load_template
45.         source, display_name = self.load_template_source(template_name, template_dirs)

File "/usr/local/lib/python2.7/dist-packages/django/template/loaders/filesystem.py" in load_template_source
39.                     return (file.read().decode(settings.FILE_CHARSET), filepath)

File "/usr/lib/python2.7/encodings/utf_8.py" in decode
16.     return codecs.utf_8_decode(input, errors, True)

Exception Type: UnicodeDecodeError at /accounts/ProfileDetails/
Exception Value: 'utf8' codec can't decode byte 0xe1 in position 5915: invalid continuation byte


base.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
     <meta charset="utf-8" />
     <title>% block title %Tagging Exploration% endblock %</title>

     <style type="text/css">

     .login_tabel tr td label
      
      color:#2E6CB8;
      font-size: 13px;
      
    </style>

</head>

<body>
<div id="website" style="border:1px solid #D910BB; width:1200px; margin:auto; padding:0px;">
% block content %

<div id="nav" style="margin-top:10px; border:1px solid #0FA31E;">
% block navigation %

% endblock %
</div>

<div id="content" style="border:1px solid #E91135;">
% block main_content %

% endblock %
</div>

% endblock %

 </div>  

 </body>
 </html>


persional_information.html

% extends "base.html" %
<script type="text/javascript">

var states = new Array();
states['Canada'] = new Array('Alberta','British Columbia','Ontario');
states['Mexico'] = new Array('Baja California','Chihuahua','Jalisco');
states['United States'] = new Array('California','Florida','New York');

var cities = new Array();
cities['Canada'] = new Array();
cities['Canada']['Alberta']          = new Array('Edmonton','Calgary');
cities['Canada']['British Columbia'] = new Array('Victoria','Vancouver');
cities['Canada']['Ontario']          = new Array('Toronto','Hamilton');
cities['Mexico'] = new Array();
cities['Mexico']['Baja California'] = new Array('Tijauna','Mexicali');
cities['Mexico']['Chihuahua']       = new Array('Ciudad Juárez','Chihuahua');
cities['Mexico']['Jalisco']         = new Array('Guadalajara','Chapala');
cities['United States'] = new Array();
cities['United States']['California'] = new Array('Los Angeles','San Francisco');
cities['United States']['Florida']    = new Array('Miami','Orlando');
cities['United States']['New York']   = new Array('Buffalo','new York');

function setStates() 
cntrySel = document.getElementById('country');
stateList = states[cntrySel.value];
changeSelect('state', stateList, stateList);
setCities();

function setCities() 
cntrySel = document.getElementById('country');
stateSel = document.getElementById('state');
cityList = cities[cntrySel.value][stateSel.value];
changeSelect('city', cityList, cityList);

function changeSelect(fieldID, newOptions, newValues) 
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++) 
selectField.options[selectField.length] = new Option(newOptions[i], newValues[i]);



function addLoadEvent(func) 
var oldonload = window.onload;
if (typeof window.onload != 'function') 
window.onload = func;
 else 
 window.onload = function() 
  if (oldonload) 
    oldonload();
  
  func();




addLoadEvent(function() 
setStates();
);
</script>

<form action="." method="post" enctype="multipart/form-data">% csrf_token %
<table border="0" align="center" cellsapcing="1" cellspadding="1">
<tr>
    <td colspan="2"><h3> Personal Information</h3></td>
</tr>

<tr>
    <td colspan="2"><input type="hidden" value=" user.id " name="user_id"   />      </td>
</tr>

<tr>
    <td>First Name</td>
    <td><input type="text" name="fname" /></td>
</tr>

<tr>
    <td>Last Name </td>
    <td><input type="text" name="lastname" /></td>
</tr>

<tr>
    <td>Address </td>
    <td><input type="text" name="address1" /></td>
</tr>
<tr>
    <td></td>
    <td><input type="text" name="address2" /></td>
</tr>

   <tr>
   <td style="text-align: left;">Country:</td>
   <td style="text-align: left;">
    <select name="country" id="country" onChange="setStates();">
     <option value="Canada">Canada</option>
     <option value="Mexico">Mexico</option>
     <option value="United States">United States</option>
   </select>
   </td>
   </tr><tr>
   <td style="text-align: left;">State:</td>
   <td style="text-align: left;">
      <select name="state" id="state" onChange="setCities();">
         <option value="">Please select a Country</option>
      </select>
   </td>
  </tr><tr>
  <td style="text-align: left;">City:</td>
  <td style="text-align: left;">
     <select name="city"  id="city">
        <option value="">Please select a Country</option>
     </select>
  </td>
  </tr>
  </table>
  </form>

【问题讨论】:

您是否将&lt;meta charset="utf-8" /&gt; 放入&lt;head&gt; 中? 我的意思是你放 javascript 的页面 在personal_information.html下面的% extends "base.html" % 您能否使用personal_information.html 内容更新您的代码。可能你把脚本放错地方了 现在已经添加了 html 【参考方案1】:

我怀疑您的模板文件未存储为 UTF-8:https://docs.djangoproject.com/en/dev/ref/unicode/#templates

如果您使用的是 Linux,您可以使用 file 命令找到文件的编码:

$ file -bi mytemplate.html
text/html; charset=utf-8

如果您的模板以 UTF-8 编码,则检查您的 settings.py 文件中 FILE_CHARSET 的值并将其设置为 "utf-8"

【讨论】:

【参考方案2】:

您从base.html 扩展personal_informtion.html,但您没有在其上使用% block %。这是一个示例。

base.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta charset="utf-8" />
    <title>% block title %Tagging Exploration% endblock %</title>

    <style type="text/css">
        .login_tabel tr td label
            color:#2E6CB8;
            font-size: 13px;
        
    </style>

    % block js %% endblock js %
</head>

<body>
..........
% block main_content %% endblock %
...........
</body>
</html>

personal_information.html

% extends "base.html" %

% block js %
     block.super 
    <script type="text/javascript">
        ..........
    <script>
% endblock %

% block main_content %
<form action="." method="post" enctype="multipart/form-data">
    % csrf_token %
    .........
</form>
% endblock %

【讨论】:

以上是关于Django:/accounts/ProfileDetails/'utf8' 处的 UnicodeDecodeError 编解码器无法解码位置 5915 中的字节 0xe1:无效的继续字节的主要内容,如果未能解决你的问题,请参考以下文章

Django之路

Django系列

django 错误

mac电脑安装django ,运行django报错解决

Django 大神带你飞系列~走进Django

django的文档