django-rest-framework将allowed_method限制为GET [英] django-rest-framework limit the allowed_methods to GET
问题描述
我刚开始使用django-rest-framework。
对此颇为热心,只是事实很少。
使api能够正常运行,但是所有额外的功能都是一个难题。
(添加额外的自定义字段等)
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 great, but all the extra's is a puzzle. (adding extra custom fields etc.)
现在,我想知道如何在例如ListView或DetailView中限制allowed_method。
将此添加到views.py中的类中,就像我在某处阅读作为答案...似乎没有任何作用:
Now I wonder how you can restrict the allowed_methods in for example a ListView or a DetailView. Adding this to the class in the views.py like I read somewhere as an answer... does not seem to have any effect:
allowed_methods = ('GET',)
推荐答案
根据近年来的投票结果可能不再相关。在'12 tho:)
Django-rest-framework实际上有很多示例。
Django-rest-framework actually have very many examples..
看看 http://django-rest-framework.org , http:// django- rest-framework.org/contents.html 和 http://rest.ep.io/
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.
如果您是自己设计REST功能,则不要使用任何django-rest-framework魔术(例如rest.ep。 io)为您生成它,您应该研究mixi n( http://django-rest-framework.org/howto/mixin.html )。
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).
如果您想限制为仅获取方法。只需使用def get(...)和mixin类。
If you want to restrict to only get methods. Just use def get(...) and the mixin class.
提供的链接示例:
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'),
)
这篇关于django-rest-framework将allowed_method限制为GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!