页面刷新后,django request.user.is_authenticated()未返回true(有时)

时间:2013-08-08 22:13:51

标签: javascript ajax django json

我有一份登记表。提交后,页面会刷新,我会根据request.user返回一些信息。有时request.user.is_authenticated()正在返回True,一切正常....有时False似乎是随机的。

我很欣赏为什么会发生这种情况。

注册表格代码

$('#reg_form').submit(function(e) {
    e.preventDefault();
    e.stopPropagation();

    var serializedData = $(this).serializeArray();
    var names = serializedData.map(function(r) {
        return r.name;
    });
    var index_user = names.indexOf("regusername");
    var index_pass = names.indexOf("regpassword1");
    var index_email = names.indexOf("regemail");

    var data2 = {};
    data2["username"] = serializedData[index_user].value;
    data2["password1"] = serializedData[index_pass].value;
    data2["password"] = serializedData[index_pass].value;
    data2["password2"] = serializedData[index_pass].value;
    data2["email"] = serializedData[index_email].value;

    console.log(data2);

    var serializedFormData = $(this).serialize();

    $.ajax({
        url: window.url_root + '/accountsjson/register/',
        type: 'POST',
        dataType: 'json',
        data: data2,
        success: function(data) {
            console.log(data); //remove
            if (data.hasOwnProperty('success')) {
                console.log("successful registration detected!!");
                utils.loginAfterRegister(data2);
                $('.register').slideUp();
                $('.frame').hide();
            } else {
                utils.showRegister();

            }
        },
        error: function() {
            console.log("ERROR posting registration request. Abort!");
        },
    });

loginAfterRegister调用的具有刷新

的函数
function sendRating(rating, reload_on_return) {
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: window.url_root + "/savecommentrating/1/" + rating.cid + "/",
            data: {
                "rating": rating.r2 / 100.0
            },
            success: function(data) {
                if (data.hasOwnProperty('success')) {
                    console.log("data was sent!");


                    if (reload_on_return) {
                        location.reload();
                    }

                }
            },
            error: function() {
                console.log("rating didn't get sent!!");
            }
        })

    }

views.py

内的移动功能
def mobile(request):
    create_visitor(request)
    os = get_os(1)
    disc_stmt = get_disc_stmt(os, 1)

    return render_to_response('mobile.html', context_instance = RequestContext(request, {'url_root' : settings.URL_ROOT,
                                             'loggedIn' : str(request.user.is_authenticated()).lower(),
                                             'client_data': mobile_client_data(request),
                                             'client_settings': get_client_settings(True),
                                             }))

create_visitor()

def create_visitor(request):
    # See if we need to create a visitor here                                                                                                                                     
    if not request.user.is_authenticated() and not request.session.get('visitor_id', False):
        visitor = Visitor()
        visitor.save()
        request.session['visitor_id'] = visitor.id

0 个答案:

没有答案