Re: Django CSRF protection and AJAX calls

2020-06-10 Thread Kevin
Thanks Boris & Allan, I was able to research the problem further and found that my header was being set entirely correctly, and the Django csrf middleware does in fact require both the cookie AND the header to be set. It's not an either/or, and there is an explicit error message for when either

Re: Django CSRF protection and AJAX calls

2020-06-10 Thread Boris Pérez
My way, i use the csrf_token tag on template, an pass it to the view in the ajax call, using $.ajax({ url : url, type : "POST", // http method data : {'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(), 'v1':v1,'v2':v2, }, // data sent with the post r

Re: Django CSRF protection and AJAX calls

2020-06-09 Thread Allan Rafael
Você pode criar um arquivo js chamado *ajax_post_config.js* e nele inserir o seguinte código: function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } function getCookie(name) { var cookieValue

Re: Django : CSRF and variable handling in a view

2011-11-24 Thread Nolhian
I guess I'll surrender and go with only the template authenticated check solution even if it still disturb me a little that the user can display the sign-up form in a page, then log in on another page and still be able to sign-up by sending the form. That's nice to know about the RequestContext ! A

Re: Django : CSRF and variable handling in a view

2011-11-24 Thread Ivo Brodien
Hi! Ah, ok. Now it makes a more sense - index was indeed confusing. Well in your template I would just make the check and show the form or not. If your don’t show the form, the user cannot make a POST to the signup view since he does not have a valid CSRF token. Anyway, if the user is logged i

Re: Django : CSRF and variable handling in a view

2011-11-24 Thread Nolhian
Hello, First of all thanks for your answer ! I think that the name I gave "index.html" is causing confusion. In fact that's the first view I created and so the only template I have at the moment and I awkwardly named it index.html, It should be named signup.html or something like that. I'm not loo

Re: Django : CSRF and variable handling in a view

2011-11-24 Thread Ivo Brodien
DrBloodMoney is right, It is kind of odd how you are solving the problem but it might me kind of right depending on what you are trying to do. Considering how most of the websites work you should do this. In all the templates include another template which does this: {% if not user.is_authentic

Re: Django : CSRF and variable handling in a view

2011-11-23 Thread Nolhian
> This seems all sorts of wrong to me. Why couldn't the user just log > out and then post? Seems like an odd workflow, but I don't know your > business-case here. Yes the user can just log out and then post but since this is a sign- up form it would seem logical to not be able to sign-up if the us

Re: Django : CSRF and variable handling in a view

2011-11-23 Thread DrBloodmoney
> If I don't check anywhere in the view if a user is authenticated, he > can still use the form to post data and my goal is that if a user is > authenticated he can't ( in the template if a user is authenticated it > doesn't display the form ). > I'm aware that it kind of defy the DRY principle bec

Re: Django : CSRF and variable handling in a view

2011-11-23 Thread Nolhian
Thanks again, Indeed that is nice to know ! Unfortunately I guess I'm still bound to use request.user.is_authenticated() in my view in this case : def index(request): if request.user.is_authenticated(): return render_to_response('index.html', {'has_account': True})

Re: Django : CSRF and variable handling in a view

2011-11-23 Thread Ivo Brodien
no problem. > I also pass has_account=True in the view if the user is authenticated. in this case in your template you can just do: {% if user.is_authenticated and user.is_active %} That is the advantage of using RequestContext. -- You received this message because you are subscribed to the G

Re: Django : CSRF and variable handling in a view

2011-11-23 Thread Nolhian
Okay thanks ! I also pass has_account=True in the view if the user is authenticated. I noticed that it evaluated it to False if it was missing, I just wanted to be sure before removing it that it was not considered to be "the best practice" to pass it anyway :) On Nov 23, 10:59 am, Ivo Brodien w

Re: Django : CSRF and variable handling in a view

2011-11-23 Thread Ivo Brodien
> What about passing a variable set to False ? Should I still pass it > like so : > return render(request,'index.html', {'form': form, > 'has_account':False}) > Or is it useless to pass it ? Since you hardcode it to be False, yes it useless also to check in the template. If the variable is missi

Re: Django : CSRF and variable handling in a view

2011-11-22 Thread Nolhian
So : return render(request,'index.html', {'form': form}) instead of ( in my 2nd question ) : c = RequestContext(request, {'has_account': False,'form': form}) return render_to_response('index.html', c) That answers my two first questions. Thanks :) What about passing a variable set to False ? Sho

Re: Django : CSRF and variable handling in a view

2011-11-22 Thread DrBloodmoney
On Tue, Nov 22, 2011 at 6:54 PM, Nolhian wrote: > Hello, > > I've got a subscription form and this view : > > def index(request): >        c = RequestContext(request) >        if request.user.is_authenticated(): >                return render_to_response('index.html', {'has_account': True}) >    

Re: Django CSRF 1.2

2010-12-07 Thread gentlestone
I'm sorry, the error was in my PERL code: $req->content("csrfmiddlewaretoken=$csrfid"); is the correct POST request setting. On Dec 7, 11:40 am, gentlestone wrote: > I have a PERL test script for DJANGO connection test. It works on > Django 1.1 admin login page, but doesn't work on 1.2. The reque

Re: django csrf

2010-06-28 Thread Tom Evans
On Fri, Jun 25, 2010 at 6:31 PM, thusjanthan wrote: > The quick answer is you have to put the following in your template > right after the declaration: > >  {% csrf_token %} > > Cheers, > Nathan. > And how precisely will that make his browser submit the form in a manner that django can decipher?

Re: django csrf

2010-06-25 Thread thusjanthan
The quick answer is you have to put the following in your template right after the declaration: {% csrf_token %} Cheers, Nathan. On Jun 25, 2:48 am, Li Hui wrote: > When I add enctype="text/plain" to a post form like method="post" enctype="text/plain">, there is a "CSRF verification > faile

Re: django csrf

2010-06-25 Thread Tom Evans
On Fri, Jun 25, 2010 at 10:48 AM, Li Hui wrote: > When I add enctype="text/plain" to a post form like method="post" enctype="text/plain">, there is a "CSRF verification > failed." error. > But when I remove it, all is right. > Who can tell me why? > Because that is not how HTML user agents work.