S
S
Sergey Nizhny Novgorod2017-07-19 14:24:43
Django
Sergey Nizhny Novgorod, 2017-07-19 14:24:43

How to refresh page with ajax request?

Hello everyone
. I'm making a "smart login" that would allow users to register through a pop-up window and stay on the same page.
Problem: in order to make friends with elements that depend on the user's authorization status, you need to refresh the page, and this does not work for me.
view

def login(request):
    if request.POST:
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        path = request.POST.get('path', '')
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request, user)
            return redirect(path)  #вот это не работает          
        else:
            json = {                
                'error_login': "нет такого пользователя",
                    }
            return JsonResponse(json, safe=False,)
    else:
        json = {
                'error_login': "произошла ошибка",
                    }
        return JsonResponse(json, safe=False,)

Ajax - a request that robs data + displays errors if something went wrong.
<script>
$('#login_form').submit(function (e) {
    e.preventDefault();
    var m_username = $('#username').val();
    var m_password = $('#password').val();
    var m_path = $('#path').val();

    $.ajax({
  type: "POST",
  url: {% url 'login' %},
  data: {
      "username": m_username,
      "password": m_password,
      "path": m_path,
      "csrfmiddlewaretoken" : "{{ csrf_token }}"
  },
  success: function (data) {
      $('#error_login_message').html(data.error_login);
  }
    });
});
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alex maslakoff, 2017-07-20
@Terras

redirect to the same page when the ajax response came.
or create a big div and refresh it.
1st option is better.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question