CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 11:58:08 GMT
content-type: text/html; charset=utf-8
cross-origin-opener-policy: same-origin
nel: {"report_to":"heroku-nel","response_headers":["Via"],"max_age":3600,"success_fraction":0.01,"failure_fraction":0.1}
referrer-policy: same-origin
report-to: {"group":"heroku-nel","endpoints":[{"url":"https://nel.heroku.com/reports?s=x5TpwKo47zup36aMCVsDKifVHUvQArSbD9x9n1TYLik%3D\u0026sid=af571f24-03ee-46d1-9f90-ab9030c2c74c\u0026ts=1760183888"}],"max_age":3600}
reporting-endpoints: heroku-nel="https://nel.heroku.com/reports?s=x5TpwKo47zup36aMCVsDKifVHUvQArSbD9x9n1TYLik%3D&sid=af571f24-03ee-46d1-9f90-ab9030c2c74c&ts=1760183888"
server: cloudflare
strict-transport-security: max-age=15552000; includeSubDomains; preload
vary: Cookie
via: 1.1 heroku-router
x-content-type-options: nosniff
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: csrftoken=q2LrmMg5lsmby1kY4sorG94GsOfQMjKP; SameSite=Lax; Path=/; Max-Age=31449600; Expires=Sat, 10 Oct 2026 11:58:08 GMT
cf-ray: 98ce2f130eac7143-BOM
alt-svc: h3=":443"; ma=86400
djangosnippets: RestView - class for creating a view that dispatches based on request.method
RestView - class for creating a view that dispatches based on request.method
Sometimes it's useful to dispatch to a different view method based on request.method - e.g. when building RESTful APIs where GET, PUT and DELETE all use different code paths. RestView is an extremely simple class-based generic view which (although it's a stretch to even call it that) which provides a simple mechanism for dividing up view logic based on the HTTP method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | """
Example usage:
class ArticleView(RestView):
def GET(request, article_id):
return render_to_response("article.html", {
'article': get_object_or_404(Article, pk = article_id),
})
def POST(request, article_id):
# Example logic only; should be using django.forms instead
article = get_object_or_404(Article, pk = article_id)
article.headline = request.POST['new_headline']
article.body = request.POST['new_body']
article.save()
return HttpResponseRedirect(request.path)
Then in your urls.py:
from my_views import ArticleView
urlpatterns = patterns('',
...
(r'^article/(\d+)/$', ArticleView()),
...
)
"""
from django.http import HttpResponse
import re
nonalpha_re = re.compile('[^A-Z]')
class RestView(object):
"""
Subclass this and add GET / POST / etc methods.
"""
allowed_methods = ('GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'OPTIONS')
def __call__(self, request, *args, **kwargs):
method = nonalpha_re.sub('', request.method.upper())
if not method in self.allowed_methods or not hasattr(self, method):
return self.method_not_allowed(method)
return getattr(self, method)(request, *args, **kwargs)
def method_not_allowed(self, method):
response = HttpResponse('Method not allowed: %s' % method)
response.status_code = 405
return response
|
More like this
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month ago
- get_object_or_none by azwdevops 4 months, 3 weeks ago
- Mask sensitive data from logger by agusmakmun 6 months, 3 weeks ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 8 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
Comments
Hah! I was sure someone else had done this before (it's an obvious approach) but my Google-fu failed me.
#
How does one use this with 'reverse'?
#
I solved this problem using url patterns with name parameter
#
Hi, How I can disable csrf protection using this snipet, I try with csrf_except but no work
#
Please login first before commenting.