我对 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'),
)
学生.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)