10

I have created a API using djangorestframework, but when i run it in terminal by using python manage.py runserver command it shows "HTTP/1.0 301 Moved Permanently" error.This the Message which it shows

But the cool thing is , when i run it in browser it displays the data . This the result in Chrome

I wanted to know had happened in it.

And in this the POST call also is not working.

Below is the code:

serializers.py

from rest_framework import serializers
from snippets.models import Snippet

class SnippetSerializer(serializers.ModelSerializer):
            class Meta:
                model = Snippet
                fields = ('title','code',)

def create(self, validated_data):
    return Snippet.objects.create(**validated_data)


def update(self, instance, validated_data):
    instance.title = validated_data.get('title', instance.title)
    instance.code = validated_data.get('code', instance.code)
    instance.save()
    return instance

Views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer


# Create your views here.


class JSONResponse(HttpResponse):
    def __init__(self,data,**kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type']='application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

@csrf_exempt
def snippet_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)

@csrf_exempt
def snippet_detail(request, pk):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        snippet = Snippet.objects.get(pk=pk)
    except Snippet.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = SnippetSerializer(snippet)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(snippet, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        snippet.delete()
        return HttpResponse(status=204)

Models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.


class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()


class Meta:
    ordering = ('created',)
MD Shahrouq
  • 609
  • 8
  • 16

4 Answers4

23

301 is not an error, but a redirect. From the command line, you requested a URL without a trailing slash, but your URL patterns expect the slash to be there. Django has a nice convenience feature to return a redirect in such cases.

Unlike your command-line HTTP client, your browser recognizes the redirect, and then follows up with a second request to the new URL including the trailing slash.

koniiiik
  • 4,240
  • 1
  • 23
  • 28
  • but @ koniiiik i am unable to POST data on to it, using postman – MD Shahrouq Dec 13 '16 at 10:45
  • 1
    You need to post to the URL that includes the trailing slash, the one without the slash will always return a redirect, and ignore the request body. – koniiiik Dec 13 '16 at 11:11
1

The effect you are seeing is because your URL is mapped with an ending / and the link you are requesting is without the ending /. Django by default will try a URL with ending /, if it cannot find a match with the requested URL.

This is controlled by the APPEND_SLASH setting, which is set to True by default:

When set to True, if the request URL does not match any of the patterns in the URLconf and it doesn’t end in a slash, an HTTP redirect is issued to the same URL with a slash appended. Note that the redirect may cause any data submitted in a POST request to be lost.

To fix this problem, change your command to:

http http://127.0.0.1:8006/snippets/
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
1

https://www.django-rest-framework.org/api-guide/routers/

"By default the URLs created by SimpleRouter are appended with a trailing slash. This behavior can be modified by setting the trailing_slash argument to False when instantiating the router. For example:

router = SimpleRouter(trailing_slash=False)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

It looks like you have APPEND_SLASH set to true. When you go to the endpoint without a slash, it automatically redirects it to the same URL with a slash, resulting in the 301.

Documentation is here.

Withnail
  • 3,128
  • 2
  • 30
  • 47