1

I'm running a Django web app and creating a REST Api using Django Rest Framework. I've created a basic list/update view however when I go to the url, I get 'A server error occurred. Please contact the administrator.' and in the console: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9735: ordinal not in range(128).

I suspect this has something to do with a template, presumable DRF's as it says position 9735.

This error isn't global as a form on a different part of my site is working perfectly.

I'm not even sure what files to show but, models.py

            from django.db import models
            from django.contrib.auth.models import AbstractUser
            from django.db import models

            # Create your models here.


            class User(AbstractUser):

                type = models.CharField(max_length = 30, null = True, default = "")


            class NewsPost(models.Model):

                title = models.CharField(max_length = 30, null = True, default = "")
                content = models.TextField(max_length = 1000, null = True, default = "")

                def __unicode__(self):
                    return u'Title is %s' % self.title

            class Emergency(models.Model):

                title = models.CharField(max_length = 30, null = True, default = "")
                content = models.TextField(max_length=1000, null=True, default="")


            class EventUpdate(models.Model):



                type = models.CharField(max_length = 30, null = True, default = "")
                status = models.CharField(max_length = 300, null = True, default = "")

views.py

            from django.shortcuts import render, render_to_response,HttpResponse, redirect
            from .forms import *
            from rest_framework import generics, mixins
            from .serializers import *
            # Create your views here.
            def events(request):
               context = {'test': 1}
               return render(request, 'events.html', context = context)


            def newspostform(request):
                if request.method == 'POST':
                    form = NewsForm(request.POST)
                    if form.is_valid():
                        form.save()
                        return redirect('/dashboard')
                    else:
                        form = NewsForm()
                else:
                    form = NewsForm()
                return render(request, 'newspostview.html', {'form': form})


            class NewsPostAPIView(generics.ListCreateAPIView):

                queryset = NewsPost.objects.all()
                serializer_class        = NewsPostSerializer

serializers.py

from rest_framework import serializers from .models import *

            class NewsPostSerializer(serializers.ModelSerializer):

                class NewsPost:
                      model = NewsPost
                      fields = (
                        'title',
                        'content',
                      )

templates in settings.py

            TEMPLATES = [
                {
                    'BACKEND': 'django.template.backends.django.DjangoTemplates',
                    'DIRS': [os.path.join(BASE_DIR, 'templates')],
                    'APP_DIRS': True,
                    'OPTIONS': {
                        'context_processors': [
                            'django.template.context_processors.debug',
                            'django.template.context_processors.request',
                            'django.contrib.auth.context_processors.auth',
                            'django.contrib.messages.context_processors.messages',
                        ],
                    },
                },
            ]

urls in app:

                            urlpatterns = [
                                    path('news-api/', NewsPostAPIView.as_view(), name = 'newsav'),
                            ]

urls in main:

 urlpatterns = [
                path('admin/', admin.site.urls),
                path('events/', views.events),
                path('newspost/', views.newspostform),
                path('api/', include('vivapi.urls')),
            ]

Other information: I'm running Django 2.2 on Python 3.6.

Any help whatsoever is appreciated. Did google this problem but only found generic information, not specific to this scenario. I also ran sys.getdefaultencoding() in shell and it returned 'utf-8'. I have created an api before, on the same system, with the same versions of both Python and Django, so I'm really clueless as to why this occurred.

Thanks, Rohan

Rohan G
  • 13
  • 3
  • What happens if you change `__unicode__` method in models to `__str__` and remove the `u` in front of the string? – karlosss Apr 23 '19 at 20:40

2 Answers2

0

Fixed: Serializers had NewsPost class insted of Meta, oops.

Rohan G
  • 13
  • 3
-3

Similar question is answered in post on stack over flow ( though it is not for Django framework ), please see if this works for you. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128)

  • 1
    The accepted answer is not related to this (almost) at all. I would have no idea where to put the `decode`. If you have some, please include it in your answer. – karlosss Apr 23 '19 at 20:42