Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a location header in Django Rest Framework

I read in a post that it is ideal to put that it is a good standard to include a Location header that points to the URL of the new resource (newly created via POST). My problem is I do not know how to include it.

I am using a class-based views using the APIView and my code in the view is:

class ListArtists(APIView):
    serializer_class = ArtistSerializer
    def get(self, request, format=None):
        _array = Artist.objects.filter()
        serializer = self.serializer_class(_array, many=True)
        if serializer.data:
            _status = status.HTTP_200_OK
        else:
            _status = status.HTTP_204_NO_CONTENT
        return Response(standardResponse(data=serializer.data), status=_status)

    def post(self, request, format=None):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(standardResponse(data=serializer.data), status=status.HTTP_201_CREATED)
        else:
            return Response(standardResponse(errors=serializer.errors))
artist = ListArtists.as_view()

urls.py

from django.conf.urls import url, include

from store import views

urlpatterns = [
    url(r'^artists/', views.artist, name='artists-list'),
]

P.S.

Every time I throw a request using my Advanced REST Client this is the response that I receive:

Date: Sat, 23 Jul 2016 10:54:23 GMT
Server: WSGIServer/0.1 Python/2.7.10
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN
Content-Type: application/json
Allow: GET, POST, HEAD, OPTIONS
like image 577
Dean Christian Armada Avatar asked Jul 23 '16 10:07

Dean Christian Armada


People also ask

How to set HTTP response header in Django project view?

The HTTP response header is always added in the Django project view function before return the HttpResponse object to the client. So edit the DjangoHelloWorld / dept_emp / views.py file home_page function as below. # set http response header and value. # return the HttpResponse object. 2.

Should I namespace my Django rest views when using hyperlinks?

Unless your models names actually clash you may be better off not namespacing your Django REST Framework views when using hyperlinked serializers. A viewset may mark extra actions for routing by decorating a method with the @action decorator. These extra actions will be included in the generated routes.

How to include a router url in Django?

The .urls attribute on a router instance is simply a standard list of URL patterns. There are a number of different styles for how you can include these URLs. For example, you can append router.urls to a list of existing views... Alternatively you can use Django's include function, like so...

How do I use simplerouter with Django?

REST framework adds support for automatic URL routing to Django, and provides you with a simple, quick and consistent way of wiring your view logic to a set of URLs. Here's an example of a simple URL conf, that uses SimpleRouter. There are two mandatory arguments to the register () method: prefix - The URL prefix to use for this set of routes.


Video Answer


1 Answers

You can add arbitrary headers to a Response object, like so:

def post(self, request, format=None):
    serializer = self.serializer_class(data=request.data)
    if serializer.is_valid():
        obj = serializer.save()
        response = Response(standardResponse(data=serializer.data), 
                            status=status.HTTP_201_CREATED)
        # If you have defined a get_absolute_url method on your model, then
        # you can use that to get a URL for the new object
        response['Location'] = obj.get_absolute_url()
        return response
like image 175
solarissmoke Avatar answered Oct 22 '22 02:10

solarissmoke