我是Django的新手并且不断得到这个错误而且不能为我的生活,找出解决方案。我想我已经包含了所有相关的可能代码部分,我们非常感谢任何帮助!当我试图打印出学校班级的所有学生时,就会出现错误。我认为错误是由与线路相关的事情引起的
render(request, 'schoolclass/students.html', context)
。以下是我的应用的相关部分以及错误消息。
schoolclass.views.py
def detail(request, schoolclass_id):
try:
student_list = Student.objects.filter(schoolclass_id = schoolclass_id).order_by('lastname')
schoolclass = SchoolClass.objects.get(id = schoolclass_id)
context = {'student_list': student_list, 'schoolclass': schoolclass}
except Student.DoesNotExist:
raise Http404
return render(request, 'schoolclass/students.html', context)
schoolclass.urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<schoolclass_id>\d+)/$', views.detail, name='detail'),
)
students.html
{% block content %}
<h1>{{ schoolclass.yearlevel }} {{ schoolclass.subject }} {{ schoolclass.description }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<table>
<tr>
<th>Last Name</th>
<th>First Name</th>
</tr>
{% for student in student_list %}
<tr>
<td>{{ student.lastname }}</td>
<td>{{ student.firstname }}</td>
</tr>
{% endfor %}
<tr>
<td>{{ student.lastname }}</td>
<td>{{ student.firstname }}</td>
</tr>
</table>
{% endblock %}
错误消息
Request Method: GET
Request URL: http://127.0.0.1:8000/schoolclass/1/
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "c:\Code\markbook\schoolclass\views.py" in detail
22. return render(request, 'schoolclass/students.html', context)
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "C:\Python27\lib\site-packages\django\template\loader.py" in render_to_string
170. t = get_template(template_name)
File "C:\Python27\lib\site-packages\django\template\loader.py" in get_template
146. template, origin = find_template(template_name)
File "C:\Python27\lib\site-packages\django\template\loader.py" in find_template
135. source, display_name = loader(name, dirs)
File "C:\Python27\lib\site-packages\django\template\loader.py" in __call__
43. return self.load_template(template_name, template_dirs)
File "C:\Python27\lib\site-packages\django\template\loader.py" in load_template
46. source, display_name = self.load_template_source(template_name, template_dirs)
File "C:\Python27\lib\site-packages\django\template\loaders\filesystem.py" in load_template_source
38. return (fp.read().decode(settings.FILE_CHARSET), filepath)
File "C:\Python27\lib\encodings\utf_8.py" in decode
16. return codecs.utf_8_decode(input, errors, True)
Exception Type: UnicodeDecodeError at /schoolclass/1/
Exception Value: 'utf8' codec can't decode byte 0x85 in position 702: invalid start byte
模型
class SchoolClass(models.Model):
user = models.ForeignKey(User)
subject = models.CharField("Subject", max_length=100, choices = SUBJECT_CHOICES, default='Select One')
yearlevel = models.CharField("Year Level", max_length=100, choices = YEARLEVEL_CHOICES, default='Select One')
description = models.CharField("Unique identifier", max_length=100, default='Maybe 2013 or school classcode')
class Student(models.Model):
schoolclass = models.ForeignKey(SchoolClass)
firstname = models.CharField(max_length=50)
lastname = models.CharField(max_length=50)
答案 0 :(得分:1)
这部分追溯:
File "C:\Python27\lib\site-packages\django\template\loaders\filesystem.py" in load_template_source
38. return (fp.read().decode(settings.FILE_CHARSET), filepath)
表示从磁盘加载模板时发生错误,而不是在渲染模板时发生错误。
此外,错误消息:
Exception Value: 'utf8' codec can't decode byte 0x85 in position 702
表示问题出在文件的702位。但是,粘贴的students.html
只有大约560个字节。因此,要么您没有粘贴整个文件,要么实际上读取的文件与您认为的文件不同。
答案 1 :(得分:0)
我认为您的模板文件编码存在问题。尝试在没有任何数据的情况下打开它(空的student_list,一些虚拟的schoolclass)。如果它仍然会抛出错误,那么问题就是模板文件本身,所以你只需要用编辑器强制输出utf-8来保存它。
否则,如果它可以在空上下文中正常工作,则需要在数据库中查找编码错误的条目。为此,您可以编写一个循环并逐个检查student_list元素。
答案 2 :(得分:0)
我还应该指出,如果文件路径中存在任何非ascii字符,也可能会出现此错误。说出类似的话:
d:\ SAGA \något\ MY_PROJECT \模板
重命名文件以仅包含ascii字符应该可以解决问题。
答案 3 :(得分:0)
我将我的主要基本html文件的编码更改为UTF16,这导致模板渲染错误,并且更改了文件编码设置。是的,我遇到了一个错误,然后落入此处antonis通过使用底部的我的编辑器(pycharm)强制将编码转换为utf 8来解决混乱,只需将当前编码转换为utf 8并错误将消失