4

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

/accounts/ProfileDetails/ 处的 UnicodeDecodeError

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

有人出主意

Traceback:

{% extends "base.html" %} 文件 "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" 在 get_response 111. response = callback(request, *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>
4

2 回答 2

4

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

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

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

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

于 2013-06-20T11:20:25.227 回答
0

你从扩展personal_informtion.html而来,base.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>

个人信息.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 %}
于 2013-04-12T10:11:55.507 回答