Forbidden (403) CSRF verification failed. Request aborted

You need to add {% csrf_token %} in your form https://docs.djangoproject.com/en/2.2/ref/csrf/ like that : <form> {% csrf_token %} <anything_else> </form> Also, you have to use RequestContext(request) everytime you use render_to_response : return render_to_response(“login.html”, {“registration_id”:registration_id}, context_instance=RequestContext(request)) And you have to import authenticate and login : from django.contrib.auth import authenticate, login

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

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