How to return an rest_framework.response object from a django custom middleware class?

I’ve just recently hit this problem. This solution doesn’t use the Django Rest Framework Response, but if your server just returns JSON this solution might work for you. New in django 1.7 or greater is the JSONResponse response type. https://docs.djangoproject.com/en/3.0/ref/request-response/#jsonresponse-objects In the middleware you can return these responses without having all the “No accepted renderers” … Read more

Python and Django OperationalError (2006, ‘MySQL server has gone away’)

Sometimes if you see “OperationalError: (2006, ‘MySQL server has gone away’)”, it is because you are issuing a query that is too large. This can happen, for instance, if you’re storing your sessions in MySQL, and you’re trying to put something really big in the session. To fix the problem, you need to increase the … Read more

Change Django Templates Based on User-Agent

Rather than changing the template directories dynamically you could modify the request and add a value that lets your view know if the user is on an iphone or not. Then wrap render_to_response (or whatever you are using for creating HttpResponse objects) to grab the iphone version of the template instead of the standard html … Read more

Execute code in Django after response has been sent to the client

The method I am going for at the moment uses a subclass of HttpResponse: from django.template import loader from django.http import HttpResponse # use custom response class to override HttpResponse.close() class LogSuccessResponse(HttpResponse): def close(self): super(LogSuccessResponse, self).close() # do whatever you want, this is the last codepoint in request handling if self.status_code == 200: print(‘HttpResponse successful: … Read more

Where to put Django startup code?

Write middleware that does this in __init__ and afterwards raise django.core.exceptions.MiddlewareNotUsed from the __init__, django will remove it for all requests :). __init__ is called at startup by the way, not at the first request, so it won’t block your first user. There is talk about adding a startup signal, but that won’t be available … Read more

Django exception middleware: TypeError: object() takes no parameters

Since you are using the new MIDDLEWARE settings, your Middleware class must accept a get_response argument: https://docs.djangoproject.com/en/1.10/topics/http/middleware/#writing-your-own-middleware You could write your class like this: from django.http import HttpResponse class ExceptionMiddleware(object): def __init__(self, get_response): self.get_response = get_response def __call__(self, request): return self.get_response(request) def process_exception(self, request, exception): return HttpResponse(“in exception”) You could also use the MiddlewareMixin to … Read more

How to set up custom middleware in Django

First: The path structure If you don’t have it you need to create the middleware folder within your app following the structure: yourproject/yourapp/middleware The folder middleware should be placed in the same folder as settings.py, urls, templates… Important: Don’t forget to create the init.py empty file inside the middleware folder so your app recognizes this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)