M
M
Maxemp2017-10-19 18:40:24
Django
Maxemp, 2017-10-19 18:40:24

Why is the login not working?

function ajax_auth(e) {
            var url = 'login';
            e.preventDefault();
            $.ajax({
                url:url,
                data:{
                    username: $("#id_username").val(),
                    password: $("#id_password").val(),
                    csrfmiddlewaretoken: '{{ csrf_token }}'
                },
                type: 'POST',
                success: function () {

                }

            })
        }

def login_user(request):
    if request.user.is_authenticated():
        return redirect('/')
    if request.is_ajax():
        if request.method == 'POST':
            user = auth.authenticate(username=request.POST.get('username'), password=request.POST.get('password'))
            if user is not None:
                auth.login(request, user)
                return redirect('/')

    return render(request, 'login/login.html', locals())

If you enter data, then nothing is written in chrome, and if you poke a lot on empty, it displays the following error:
login:65 Uncaught TypeError: Cannot read property 'preventDefault' of undefined
at ajax_auth (login:65)
at HTMLButtonElement.onclick (login :31)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question