--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5345976E.7090406%40jfcomputer.com.
For more options, visit https://groups.google.com/d/optout.
$(document).ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
function safeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e11203ae-0ca6-4565-a891-33fae5e021f4%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5345B90A.8020703%40jfcomputer.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5345D112.3010800%40jfcomputer.com.
I have created a csrf.js added the code.
I added it to the html base
<script type="text/javascript"
src="/site_media/js/csrf.js"></script>
I've added the {% csrf_token %} in the form.
Where oh where do I use the code or is it magic!
Does it really work?
I don't mean to be an ass but using google search I
find over 20,000 hits on the django and csrf on several of the
sites. Stackoverflow has 12,000 + on one page about this
http://stackoverflow.com/questions/6506897/csrf-token-missing-or-incorrect-while-post-parameter-via-ajax-in-django/6533544#6533544
This might be the greatest thing since slice bread
but I can't figure out how to use it!
At this point I'm willing to pay some guru!
And below is what I get from debug.
Reason given for failure:
CSRF cookie not set.
In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
RequestContext for the template,
instead of Context.{% csrf_token %} template tag inside
each POST form that targets an internal URL.CsrfViewMiddleware, then you must
use csrf_protect on any views that use
the csrf_token template tag, as well
as those that accept the POST data.You're seeing the help section of this page because you
have DEBUG = True in your Django settings
file. Change that to False, and only the
initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW
setting.
Johnf