简体   繁体   中英

Django: is it possible to specify accept method for a view in urls

I'm curious about setting urls accept_method directly when defining url like

url(r'some-pattern', 'view_name', name='url_name', accept_method='GET|POST')

I know there're view decorators like require_GET , require_POST etc.

Is there any solutions for Django or any extension to do it?

Thanks.

Yes it can be done using django middleware.

Use Process Request to intercept the requests.

It should return either None or an HttpResponse object. If it returns None, Django will continue processing this request, executing any other process_request() middleware, then, process_view() middleware, and finally, the appropriate view. If it returns an HttpResponse object, Django won't bother calling any other request, view or exception middleware, or the appropriate view; it'll apply response middleware to that HttpResponse, and return the result.

HttpRequest has a path attribute which contains the url. Use that url to lookup the urlconf and check the urlconf params for the accept-method and return None or the same HttpResponse accordingly.

@Pratik Mandrekar thanks for definitive information and documentation.

I've created a small application which does this kind of job https://github.com/imanhodjaev/django-besmart-common

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM