首先,我已经尝试了所有可以找到SO和google的方法,但没有一个工作,所以问这里。
我正在学习django
并做一个测试项目。一切进展顺利但在我尝试登录管理面板后创建superuser
时显示UnicodeDecodeError
。我尝试了几种方法,但不断收到错误。我在使用python 3的32位Windows 7上使用django 1.11
我创建了一个应用students
并注册了该应用。然后运行migrate
。
学生/ models.py
from django.db import models
class Students(models.Model):
roll = models.IntegerField(primary_key=True)
name = models.CharField(max_length=200)
dept = models.CharField(max_length=200)
inst = models.CharField(max_length=200)
def __str__(self):
return self.name + "("+ self.dept + ")"
学生/ views.py
from django.shortcuts import render
from django.views.generic.base import View
from students.models import Students
class StudentListView(View):
def get(self,request):
students = Students.objects.all()
return render(request,'students/index.html',{'students':students})
urls.py
from django.conf.urls import url
from django.contrib import admin
from students.views import StudentListView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^students/',StudentListView.as_view()),
]
有人可以帮我解决问题吗?
修改:
这是traceback
。
(VE) F:\Virtual Environments\VE\djangogirls\myprojects>python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
May 25, 2017 - 19:23:58
Django version 1.11.1, using settings 'myprojects.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[25/May/2017 19:23:59] "GET /admin/ HTTP/1.1" 302 0
Internal Server Error: /admin/login/
Traceback (most recent call last):
File "F:\VIRTUA~1\VE\lib\site-packages\django\core\handlers\exception.py", lin
e 41, in inner
response = get_response(request)
File "F:\VIRTUA~1\VE\lib\site-packages\django\core\handlers\base.py", line 217
, in _get_response
response = self.process_exception_by_middleware(e, request)
File "F:\VIRTUA~1\VE\lib\site-packages\django\core\handlers\base.py", line 215
, in _get_response
response = response.render()
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\response.py", line 107,
in render
self.content = self.rendered_content
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\response.py", line 82,
in rendered_content
template = self.resolve_template(self.template_name)
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\response.py", line 64,
in resolve_template
return select_template(template, using=self.using)
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\loader.py", line 48, in
select_template
return engine.get_template(template_name)
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\backends\django.py", li
ne 39, in get_template
return Template(self.engine.get_template(template_name), self)
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\engine.py", line 162, i
n get_template
template, origin = self.find_template(template_name)
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\engine.py", line 136, i
n find_template
name, template_dirs=dirs, skip=skip,
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\loaders\base.py", line
38, in get_template
contents = self.get_contents(origin)
File "F:\VIRTUA~1\VE\lib\site-packages\django\template\loaders\filesystem.py",
line 29, in get_contents
return fp.read()
File "F:\VIRTUA~1\VE\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x98 in position 402714: inv
alid start byte
[25/May/2017 19:24:01] "GET /admin/login/?next=/admin/ HTTP/1.1" 500 114793
答案 0 :(得分:1)
将这些行添加到模型文件的顶部并查看文件
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
也可以将 str 方法设置为:
def __str__(self):
return "(%s)" % self.name
现在,再试一次。
答案 1 :(得分:0)
可以this帮助。
def __unicode__(self):
return u"{} ({})".format(self.name, self.dept)
答案 2 :(得分:0)
尝试编码为utf-8。
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
def __unicode__(self):
return u"{} ({})".format(self.name, self.dept)
但是,它似乎是一个特定于环境的问题。