Q:

django ajax without jquery

from django.http import JsonResponse

def get_random_number_json(request):
    random_no = random.randint(1,100)
    return JsonResponse({'random_no': random_no})
1
index.html
    $("#id_username").blur(function () {
      var form = $(this).closest("form");
      $.ajax({
        method: "POST",
        url: form.attr("data-hashtag-url"),
        data: {
          name: $('#id_username').val()
        },
        dataType: 'json',
      });

    });

views.py
def validate_username(request):
    if request.method == 'POST':
        user = request.user
        hashtag = request.POST['name']
        if HashTag.objects.filter(name__iexact=hashtag).exists():
            return False
        else:
            HashTag.objects.create(
                user=user,
                name=hashtag
            )
4

New to Communities?

Join the community