1

First of all I've tried all the ways I could find out on SO and google but none worked, so asking here.

I'm learning djangoand doing a test project. Everything was going well but after creating superuser when I tried to log into admin panel it showed UnicodeDecodeError. I've tried several method but kept getting the error. I'm using django 1.11 on windows 7 32 bit with python 3.
I have created an app students and registered the app. Then ran migrate.

students/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 + ")"

students/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()),
]

Can someone help me to solve the issue?

EDIT:
Here is the 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
Tasfia Sharmin
  • 389
  • 1
  • 7
  • 23

3 Answers3

1

add these line in the top of your model file and view file

# -*- encoding: utf-8 -*-

from __future__ import unicode_literals

too, set str method equal:

def __str__(self):
   return "(%s)" % self.name

now, try again to run.

0

May this help.

def __unicode__(self):
    return u"{} ({})".format(self.name, self.dept)
khue bui
  • 1,366
  • 3
  • 22
  • 30
0

Try encoding to utf-8.

# -*- encoding: utf-8 -*-
from __future__ import unicode_literals


def __unicode__(self):
    return u"{} ({})".format(self.name, self.dept)

However, it seems like an environment specific issue.

Md. Al-Amin
  • 1,423
  • 1
  • 13
  • 26