Why don't you just use? url(r'^api/article/$', Articleas_view(), name='article'),
Depending on the HTTP method the request will be routed to the relevant method. Or if you want to have different urls per method you can create separate class based views, I would also check out the generic views. They are just like class based views (http://www.django-rest-framework.org/api-guide/generic-views/). If you for any reason want to stick with this implementation and only allow HTTP request methods on the specified urls you could do that with a middleware. Which would look something like this: class CheckUrlMiddleware(object): def __init__(self): self.exceptions = tuple(re.compile(url) for url in settings.LOGIN_REQUIRED_URLS_EXCEPTIONS) def process_view(self, request, view_func, view_args, view_kwargs): url = resolve(request.path_info) if url.url_name *== '*get_article*' and not request.method == 'GET':* * raise *PermissionDenied() I did not tried the code above it is just to give you an example. Regards, Norbert. On Tuesday, November 1, 2016 at 5:56:50 PM UTC+2, Jonathan DEKHTIAR wrote: > > Hello fellow developpers, > > I have a quick question about implementing views and urls in the best > manner > > I currently have this : > > > - *views.py:* > > > from rest_framework.views import APIView > from rest_framework.response import Response > > class Article(APIView): def get(self, request): > #Perform Sexy Operations > return Response({"message": "Hello, world!"}) > > def post(self, request): > #Perform Sexy Operations > return Response({"message": "Hello, world!"}) > > def put(self, request): > #Perform Sexy Operations > return Response({"message": "Hello, world!"}) > > def delete(self, request): > #Perform Sexy Operations > return Response({"message": "Hello, world!"}) > > > - *urls.py:* > > > #!/usr/bin/env python > # -*- coding: utf-8 -*- > > from __future__ import unicode_literals > from django.conf import settings > from django.conf.urls import include, url > import django.contrib.auth.views > > from .views import * > > urlpatterns = [ > url(r'^api/get/article/$', Articleas_view(), name='get_article'), > url(r'^api/post/article/$', Article.as_view(), name='post_article'), > url(r'^api/modify/article$', Article.as_view(), name='modify_article'), > url(r'^api/delete/article$', Article.as_view(), name='delete_article'), > ] > > > *My Question :* > > How to make sure that *ONLY:* > > - *GET *Requests can *ONLY *be route with the "api/get/article/" route. > - *POST *Requests can *ONLY *be route with the "api/post/article/" > route > - And so on ... > > Because, as it's designed, nothing prevent me to trigger the "get" > behavior from the "post" route. > Which is not a super big deal, however, I'd prefer to forbid it. > > Is Class-Based views the best way to implement views ? I find it very cool > and intuitive to be honest. > > Thanks a lot for your answers and have a good day. > > Jonathan DEKHTIAR > -- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
