0

I'm working on a web app using the Django rest framework tutorial. I'm trying to load JSON data on my project site using the dev server. However, every time I go to the address, it returns a HTTP 500 error with an AttributeError that says: 'module' object has no attribute 'PersonsList'.

I followed the entire tutorial, edited the urls and views py files a bunch of times, yet I keep getting the same error. Any suggestions of how to fix this?

Here's my code.

urls.py

from django.conf.urls import patterns, url
from pros import views

urlpatterns = patterns ('',
    url(r'^pros/$', views.PersonsList.as_view(), name='PersonsList')

    )

models.py

from django.db import models

# Create a "Persons" class to add new input field datatypes 
class Persons(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=False)
    language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
    style  = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)


class Meta:
    ordering = ('created',)

serializers.py

from django.forms import widgets
from rest_framework import serializers
from persons.models import Persons, LANGUAGE_CHOICES, STYLE_CHOICES


class PersonsSerializer(serializers.Serializer):
    pk = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')


    def create(self, validated_data):
        """
        Create and return a new 'Persons' instance, given the validated data.
        """
        return Persons.objects.create(**validated_data)


    def update(self, instance, validated_data):
        """
        Update and return an existing 'Persons' instance, given the validated data.
        """
        instance.title = validated_data.get('title', instance.title)
        instance.code = validated_data.get('code', instance.code)
        instance.linenos = validated_data.get('linenos', instance.linenos)
        instance.language = validated_data.get('language', instance.language)
        instance.style = validated_data.get('style', instance.style)
        instance.save()
        return instance

views.py

from pros.models import Persons
from pros.serializers import PersonsSerializer
from django.http import Http404 
from django.http import HttpResponse
from rest_framework.views import APIView 
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from rest_framework import mixins
from rest_framework import generics



class PersonsList (mixins.ListModelMixin,
                   mixins.CreateModelMixin,
                   mixins.DestroyModelMixin,
                   generics.GenericAPIView):
    queryset = Persons.objects.all()
    serializer_class = PersonsSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)


class PersonsDetail (mixins.RetrieveModleMixin,
                     mixins. UpdateModelMixin,
                     mixins.DestroyModelMixin,
                     generics.GenericAPIView):
    queryset = Persons.objects.all()
    serializer_class = PersonsSerializer


def get(self, request, *args, **kwargs):
    return self.retrieve(request, *args, **kwargs)

def put(self, request, *args, **kwargs):
    return self.update(request, *args, **kwargs)

def delete(self, request, *args, **kwargs):
    return self.destroy(request, *args, **kwargs)


@api_view(['GET', 'POST'])
def persons_list(request):
    def persons_list(request, format=None):
        """List all persons, or create a new person
        """

    if request.method == 'GET':
        persons = Persons.objects.all()
        serializer = PersonsSerializer(persons, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = PersonsSerializer(data=request.data)
    if serializer.is_valid():
            serializer.save()
            return Response(serializer.data status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

directorystructure.txt

zelmundo
  • 61
  • 1
  • 1
  • 7
  • what version of django are you using? – NightShadeQueen Jul 23 '15 at 17:59
  • I'm using version 1.8.2 – zelmundo Jul 23 '15 at 18:01
  • Could you provide directory structure of the project? – f43d65 Jul 23 '15 at 18:11
  • The `views.py` you are showing is inside the `pros/` folder? Because that's where it's looking for it based on `from pros import views`, you might be importing the wrong `views.py` given the error message – bakkal Jul 23 '15 at 18:11
  • f43d65, my directory structure? If you're asking about my file path its C:\Users\username\PROS – zelmundo Jul 23 '15 at 18:15
  • Directory structure means showing where all the project files are located relative to each other (like a tree output), see my question e.g. – bakkal Jul 23 '15 at 18:17
  • Yes bakkal it is in the `pros/` folder, you may be correct. I have a _views.py_ file that I did for the tutorial, then I created another for the web app. Should I test this by blocking the code in the _views.py_ file from the tutorial as comments ? – zelmundo Jul 23 '15 at 18:18
  • Well, the error says `PersonsList` wasn't found, supposedly while looking for it in `pros.views` module so I'd double check you have pointed it to a `views` module that has `PersonsList` – bakkal Jul 23 '15 at 18:20
  • Directory structure is something similar to this http://stackoverflow.com/a/11222631/4907653 . You can use `tree` command to get it. – f43d65 Jul 23 '15 at 18:22
  • Oh okay, I edited my post and shared my directory structure through dropbox. It's pretty long. Scroll up and follow the link at the bottom. – zelmundo Jul 23 '15 at 21:01

0 Answers0