Giuseppe
Giuseppe

Reputation: 393

Switch view on same URL but different method in django with rest framework?

What is the most "pythonic" way to handle view routing on same url based on method? i don't like the solution

if(request.method == 'GET'):
    .......

is there a better way?

Upvotes: 3

Views: 4435

Answers (1)

Gianmar
Gianmar

Reputation: 515

Django View is the most pythonic code.

from django.http import HttpResponse
from rest_framework.views import APIView

class MyView(APIView):
    def get(self, request):
        # <view logic>
        return HttpResponse('result')
    def post(self, request):
        # <view logic x2>
        return HttpResponse('message_post_template')

urls.py

from django.conf.urls import url
from myapp.views import MyView

urlpatterns = [
    url(r'^about/$', MyView.as_view(), name='view'),
]

Upvotes: 6

Related Questions