1

我有一个注册表。提交后,页面刷新,我根据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
4

0 回答 0