Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 505
Giuseppe Avatar asked Dec 08 '25 18:12

Giuseppe


1 Answers

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'),
]
like image 183
Gianmar Avatar answered Dec 10 '25 21:12

Gianmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!