django-rest-framework limit the allowed_methods to GET

后端 未结 4 1398
名媛妹妹
名媛妹妹 2020-12-31 11:04

I Have just started with django-rest-framework. Pretty enthousiastic about it, except for the fact there are very little examples available. getting the api working is going

4条回答
  •  甜味超标
    2020-12-31 11:20

    Probably not relevant anymore based on recent years downvotes.. It was relevant in '12 tho :)

    Django-rest-framework actually have very many examples..

    Take a look at http://django-rest-framework.org, http://django-rest-framework.org/contents.html and http://rest.ep.io/ for some good examples and documentation.

    If you are designing a REST function by yourself, not using any of the django-rest-framework magic (like rest.ep.io) to generate it for you, you should look into mixin (http://django-rest-framework.org/howto/mixin.html).

    If you want to restrict to only get methods. Just use def get(...) and the mixin class.

    Example from link provided:

    curl -X GET http://rest.ep.io/mixin/
    

    urls.py

    from djangorestframework.compat import View
    from djangorestframework.mixins import ResponseMixin
    from djangorestframework.renderers import DEFAULT_RENDERERS
    from djangorestframework.response import Response
    
    from django.conf.urls.defaults import patterns, url
    from django.core.urlresolvers import reverse
    
    
    class ExampleView(ResponseMixin, View):
        renderers = DEFAULT_RENDERERS
    
        def get(self, request):
            response = Response(200, {'description': 'Some example content',
                                      'url': reverse('mixin-view')})
            return self.render(response)
    
    
    urlpatterns = patterns('',
        url(r'^$', ExampleView.as_view(), name='mixin-view'),
    )
    

提交回复
热议问题