Search code examples
pythondjangovalidationdefault-constructor

django : How to create & call a Constructor on every request?


I want to call a request validator method every time a POST request is called.

So Code I want to have like :

import validator

class ViewClass():
    def __CONSTRUCTOR__(self, request):
        is_valid = validator.validate()
        if is_valid == FALSE
           return HttpResponse('Request is Invalid')

    def request_function_one(request):
        if request.method == 'POST':
            return HttpResponse('Request 1 is Valid')

    def request_function_two(request):
        if request.method == 'POST':
            return HttpResponse('Request 2 is Valid')

How can I achieve this using django ?


Solution

  • If you really really want to do it in django - I recommend to look on http://www.django-rest-framework.org/

    You will not be dissapointed by power of validators in this framework.

    http://www.django-rest-framework.org/api-guide/validators/