我正在使用this django snippet [1]重定向到我的https网站。我知道它不适用于POST请求,所以我没有在{'SSL': True }
中的所有url POST请求中添加urls.py
。但是我总是得到301错误(在我的访问日志中),尽管视图执行了它要求做的事情。
视图由ajax请求“调用”并返回一个json对象。
def confirm(request, username):
if username == request.user.username:
user = get_object_or_404(User, username=username)
if request.method == 'POST':
response_dict = {
'status': True,
'added_total': count,
'username': username,
'message': message
}
return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
else:
return HttpResponseRedirect('/fx/%s?page=%s' % (username, '1'))
else:
raise Http404(u'Not a valid user')
我还尝试添加{'SSL': True}
参数,但仍然遇到301错误。
对于ajax发布请求我也使用此代码段:
$.ajaxSetup({
beforeSend:function (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;
}
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});
我做错了什么?
编辑:这是我urls.py
的行动
url(r'^confirm/(\w+)/$', confirm),