TransWikia.com

'str' object is not callable in django

Stack Overflow Asked by Myrzabek on November 12, 2021

I’m newbie in Django. I get the TypeError ‘str’ object is not callable in my Django Project when I go browsable api in url "localhost:8000/api/questions/".I’m using Django 3.I don’t know how to debug this error so here I write. I think I’m doing something wrong with urls

main urls.py

path('accounts/', 
    include('django.contrib.auth.urls')
),

path('accounts/', 
    include('django_registration.backends.one_step.urls')
),
path('api/', 
    include('users.api.urls')
),
path('api/', 
    include('questions.api.urls')
)

urls.py in app

from rest_framework.routers import DefaultRouter

from questions.api.views import QuestionViewSet

router = DefaultRouter()
router.register(r'questions', QuestionViewSet)

urlpatterns = [
    path('', include(router.urls))
]

views.py in app

from rest_framework.permissions import IsAuthenticated

from questions.api.permissions import IsAuthorOrReadOnly
from questions.api.serializers import QuestionSerializer, AnswerSerializer
from questions.models import Question
    

class QuestionViewSet(viewsets.ModelViewSet):
    queryset = Question.objects.all()
    lookup_field = 'slug'
    serializer_class = QuestionSerializer
    permission_classes = ['IsAuthenticated','IsAuthorOrReadOnly']

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

serializers.py in app

from rest_framework import serializers
from questions.models import Answer, Question

class QuestionSerializer(serializers.ModelSerializer):
    author = serializers.StringRelatedField(read_only=True)
    created_at = serializers.SerializerMethodField()  
    slug = serializers.SlugField(read_only=True)    
    answer_count = serializers.SerializerMethodField()
    user_has_answered = serializers.SerializerMethodField()

    class Meta:
        model = Question
        exclude = ['updated_at']


    def get_created_at(self, instance):
        return instance.created_at.strftime('%B %d, %Y')

    def get_answer_count(self, instance):
        return instance.answers.count()  

    def get_user_has_answered(self, instance):
        request = self.context.get('request')
        return instance.answers.filter(author=request.user.pk).exists()    

structure of my project

/project
    /questions
        /api   
            serializers.py
            views.py
            urls.py

Error traceback

TypeError at /api/questions/
'str' object is not callable
Request Method: GET
Request URL:    http://localhost:8000/api/questions/
Django Version: 3.0.8
Exception Type: TypeError
Exception Value:    
'str' object is not callable
Exception Location: /home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in <listcomp>, line 278
Python Executable:  /home/myrza/Desktop/ff/django/udemyfinpr/venv/bin/python
Python Version: 3.6.9
Python Path:    
['/home/myrza/Desktop/ff/django/udemyfinpr/QuestionTime',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages']
Server time:    Tue, 21 Jul 2020 15:05:47 +0000
Traceback Switch to copy-and-paste view
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/django/core/handlers/exception.py in inner
            response = get_response(request) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py in wrapped_view
        return view_func(*args, **kwargs) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/viewsets.py in view
            return self.dispatch(request, *args, **kwargs) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
            response = self.handle_exception(exc) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in handle_exception
            self.raise_uncaught_exception(exc) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in raise_uncaught_exception
        raise exc …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
            self.initial(request, *args, **kwargs) 

One Answer

Urlpatterns must in your case be equal to router.urls:

urlpatterns = router.urls

Answered by Gaspard Merten on November 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP