Ok, I'm going to reverse engineer this answer so that you understand where I come from. The CSRF tool works as follows:
You make request -------> request hits csrf --(invalid/no token)--> render 403 middleware | (valid token) | \ / Call view | \ / middleware sets csrf cookie | \ / Response appears
In other words, if you see the 403 csrf page, your opinion has never been called up. You can confirm this by inserting a false print statement in the view and looking at the output from runserver when you make your request.
To solve this problem, you need to either disable csrf (not good) or use one of the ajax methods available to you . If the required token is passed in your view, it will actually be executed.
The reason your opinion is not being called is to prevent the reality from the fake website from ever happening - for example, if you reject the template during the response, the user will already be logged in. The same thing happens with Decorators functions.
As for middleware, a cookie that does not change at all or is independent of the rendering function sets the HTTP Cookie: ... header Cookie: ... in the response. All answers in Django are HttpResponse objects until they finally convert them to output; render functions are helpers, but that is not what causes your problem here.
Change I will transform what you have into call rendering. You can do it:
return render_to_response(`ajax_templates/login_response.html`, {'loginresponse': json.dumps(result)})
Where ajax_templates/login_response.html true:
{% loginresponse %}
What is it. HttpResponse has a basic default argument that returns a string (literally, html web page); what are you doing initially. render_to_response and render are shortcuts for this that do this:
render_to_response called ----> open template asked for --> substitute arguments | \ / django instructs web server <--- return this from view <-- create HttpResponse to send to client object
user257111
source share