1

I have a Django 2.2 that has two apps - status and updates:

django_api | status | api | updates

In django_api/urls.py:

from django.contrib import admin
from django.urls import include, path

from updates.views import (
    json_example_view, 
    JsonCBV, 
    JsonCBV2, 
    SerializedListView, 
    SerializedDetailView
)

from status.api.views import 
StatusListSearchAPIView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/status/', status.api.urls),
    path('api/updates/', updates.api.urls),

In django_api/status/api/views.py:

from rest_framework import generics
from rest_framework.views import APIView 
from rest_framework.response import Response
from .serializers import StatusSerializer 
from status.models import Status

class StatusListSearchAPIView(APIView):
    permission_classes = []
    authentication_classes = []

    def get(self, request, format = None):
        qs = Status.objects.all()
        serializer = StatusSeralizer(qs, many = True)
        return Response(serializer.data)

    def post(self, request, format=None):
        qs = Status.objects.all()
        serializer = StatusSeralizer(qs, many = True)
        return Response(serializer.data)

class StatusAPIView(generics.ListAPIView):
    permission_classes = []
    authentication_classes = []
    queryset = Status.objects.all()
    serializer_class = StatusSeralizer

    def get_queryset(self):
        qs = Status.objects.all()
        query = self.request.GET.get('q')
            if query is not None: 
                qs = qs.filter(content_icontains = query)
            return qs 

In django_api/status/api/serializers.py:

from rest_framework import serializers
from django import forms
from status.models import Status

class StatusSerializer(serializers.ModelSerializer):
     class Meta: 
         model = Status
         fields = [
            'user',
            'content',
            'image']

     def validate_content(self, value):
         if len(value) > 10000:
             raise serializers.ValidationError("This is way too long.")
             return value

     def validate(self, data):
         content = data.get("content", None) 
         if content == "":
             content = None
         image = data.get("image", None)
         if content is None and image is None:
             raise serializers.ValidationError("content or image is required.")
        return data

When I run python manage.py runserver, I am getting an error:

File "...\status\api\views.py", line 26, in StatusAPIView
serializer_class = StatusSeralizer
NameError: name 'StatusSeralizer' is not defined

Updated: as indicated, this was a spelling error. However, after I imported status and from status.api imported urls, I am getting this error:

...\urls.py", line 39, in <module>
path('api/status/', status.api.urls),
...File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site- 
packages\django\urls\conf.py", line 73, in _path
raise TypeError('view must be a callable or a list/tuple in the case of 
include().')
TypeError: view must be a callable or a list/tuple in the case of 
include().

This is my status/api/urls.py:

from django.contrib import admin
from django.urls import path

from .views import StatusAPIView, StatusCreateAPIView


 urlpatterns = [
    path('/', StatusAPIView.as_view()),
    path('create/', StatusCreateAPIView.as_view()),
    ]

Why am I getting this error?

6
  • Did you try adding import status into your urls.py? Commented Apr 18, 2020 at 3:06
  • @kamilyrb getting a new error: "...\urls.py", line 38, in <module> path('api/status/', status.api.urls), AttributeError: module 'status.api' has no attribute 'urls'" Commented Apr 18, 2020 at 3:26
  • Have you a urls.py file in your status module? Commented Apr 18, 2020 at 3:33
  • it is under status/api/urls.py. When I imported it, I got new error: TypeError: view must be a callable or a list/tuple in the case of include(). Commented Apr 18, 2020 at 3:35
  • 1
    Okey, i found the problem. If you want to add urls of another apps, you must use include function like that :path('api/status',include('status.api.urls')) please try ir Commented Apr 18, 2020 at 3:42

3 Answers 3

1

It's a typo:

You are calling: StatusSeralizer but it is: StatusSerializer

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

1 Comment

That fixed it, but I've got a new error: NameError: name 'status' is not defined. See the updated question.
1

I dont see StatusCreateAPIView in you views file. Have you created this view? Try just commenting or removing path('create/', StatusCreateAPIView.as_view()), and see If you still get an error.

Comments

0

Try it bro:

from rest_framework import status.

Tell me if you have sucess!

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.