B
B
Bruceee2019-01-16 16:55:20
Django
Bruceee, 2019-01-16 16:55:20

How to make analog of https://namechk.com/ with Django?

Hello!
Can you please tell me how to make asynchronous calls to without reloading the page, like on the site https://namechk.com/ using Django?
I would like my backend to check for a free nickname, and the page itself accessed this backend asynchronously and received a response.
It is not very clear how a multiple asynchronous call of free name checks is made, then this data is obtained and displayed? On the above site, you can see that for each of the checked services, the answer comes at a different time and after that it is immediately displayed (the square changes color).
If possible, I would like to understand how to build the architecture of such a site, preferably as much python and server work as possible, and as little JS as possible.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-01-16
@Bruceee

Something like this:

urlpatterns = [
    path('check-username/', check_username, name='check_username'),
]

@require_GET
def check_username(request):
    if 'name' not in request.GET:
        return HttpResponseBadRequest()

    return JsonResponse({'exists': User.objects.filter(username__iexact=request.GET['name']).exists()})

$('#id_name').change(function() {
    $.getJSON('/check-username/', {'name': $(this).val()}, function(user) {
        if(user.exists)
            alert('Пользователь с таким именем уже зарегистрирован');
    });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question