0

When I try to access http://localhost:8000/api/articles/ it works fine.

When I try to access http://localhost:8000/api/articles/1 it works fine.

When I try to access http://localhost:8000/api/articles/create Django thinks that I am trying to perform a GET request ('get': 'retrieve'). What am I doing wrong?

errors invalid literal for int() with base 10: 'create'

urls.py

urlpatterns = [
    path('', ArticleViewSet.as_view({'get': 'list'}), name='list'),
    path('<pk>/', ArticleViewSet.as_view({'get': 'retrieve'}), name='detail'),
    path('create/', ArticleViewSet.as_view({'post': 'create'}) ,name='create'),
]

views.py

class ArticleViewSet(ViewSet):
    queryset = Article.objects.all()

    def list(self, request):
        articles = query_filter(request, ArticleViewSet.queryset)
        serializer = ArticleSerializer(articles, many=True)
        articles = formatter(serializer.data)
        return Response(articles)

    def retrieve(self, request, pk=None):
        article = get_object_or_404(ArticleViewSet.queryset, pk=pk)
        serializer = ArticleSerializer(article, many=False)
        article = formatter([serializer.data])
        return Response(article)

    def create(self, request):
        articles = ArticleViewSet.queryset
        articles.create(title=request.data['title'], body=request.data['body'])
        article = articles.last()
        serializer = ArticleSerializer(article, many=False)
        article = formatter([serializer.data])
        return Response(article)

Also when I switch the positions of retrieve and create in the urlpatterns shown below, I get this error "detail": "Method \"GET\" not allowed.".

urlpatterns = [
    path('', ArticleViewSet.as_view({'get': 'list'}), name='list'),
    path('create/', ArticleViewSet.as_view({'post': 'create'}), name='create'),
    path('<pk>/', ArticleViewSet.as_view({'get': 'retrieve'}), name='detail'),
]
2
  • Shouldn't articles.create( be articles.objects.create(? Dec 3, 2018 at 0:24
  • @RedCricket I'll fix that but that doesn't seem to be the problem. Dec 3, 2018 at 0:41

3 Answers 3

2

When you are trying with http://localhost:8000/api/articles/create, you are actually making GET request. That is why you are seeing the error("detail": "Method \"GET\" not allowed.".). If you want to make post request, then you need to use api tools like postman. If you use postman, try like this:

enter image description here

And your second url pattern is correct. because if you keep <pk>/ before create/, django interprets that you are calling <pk>/(should be <int:pk>)url with argument create(which is a string), when you are actually calling the create method. And when it tries to convert it to integer(as primary key is an autofield), it throws invalid literal for int() with base 10: 'create' exception.

1

Create method doesn't not support get action that's why you're getting error {"detail": "Method \"GET\" not allowed."}. Alternative you can try ModelViewset that provides default create(), retrieve(), update(), partial_update(), destroy() and list() actions.

Either you can create get_serailzer() method using that you can get browsable API with JSON and HTML form through that you can do POST action.

class ArticleViewSet(viewsets.ViewSet):
    queryset = Article.objects.all()

    def get_serializer(self, *args, **kwargs):
        return ArticleSerializer(*args, **kwargs)

    def create(self, request, *args, **kwargs):
        articles = ArticleViewSet.queryset
        articles.create(title=request.data['title'], body=request.data['body'])
        article = articles.last()
        **serializer = self.get_serializer(article, many=False)**
        from pyreadline.logger import formatter
        article = formatter([serializer.data])
        return Response(article)

Browsable API Image

0

Solution: I was using slashes at the end and that when I was sending request I wasn't adding slashes at the end. So simply I deleted end slashes from django urls

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.