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
4
  • @Alasdair I've updated the code. Please have a look. Commented May 25, 2017 at 13:33
  • Did you created superuser using any bangla name or something? How did you create the user? Try deleting the user and create new superuser using './manage.py createsuperuser' and login again Commented May 26, 2017 at 16:26
  • @ruddra I created superuser using python manage.py createsuperuser command and username is in english. Commented May 27, 2017 at 2:41
  • Did you erver end up resolving this issue? Commented May 21, 2019 at 15:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

May this help.

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.