Monday, January 7, 2019

django middleware return response and prevent calling main function

I want to use a form of cache using django middleware/context-processors. I do know it's simpler to just add the decorator at the top of the function but for reasons I have to do it this way.

Using this as my example of my function

    def cache_results(request):
        response =  {}
        if request.path == '/about':
            #my json response
            return {...}

        return response

the idea is if it matches my requests it returns a result and also prevent the matching function call from the `urls.py` from being called or returning the result basically acting as a middleware caching system.

the views look like this

    def about(request):
        response = {
            'title': 'This is the About Page',
            'activity': 'Check out this link ------',
            'additional data': 'something else'
        }

        return HttpResponse(
                json.dumps(response),
                content_type="application/json"
            )


Is this doable?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2d289f89-127c-4c36-89d8-c4ac5d71e939%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment