Как я могу преобразовать обычную отправку метода post из html в вызов ajax в django без потери той же логики?

Как я могу преобразовать обычный метод отправки сообщения из html в вызов ajax в django без потери той же логики.Мои файлы django приведены ниже.Я не хочу потерять логику. Логика должна быть такой же.

views.py

@csrf_exempt
@require_http_methods(['POST', 'GET']) # only get and post
def crawl(request):
    if request.method == 'POST':   
        url = request.POST.get("id_url",None)
        print(url)
        
        if not url:
            return JsonResponse({'error': 'Missing  args'})
        if not is_valid_url(url):
            return JsonResponse({'error': 'URL is invalid'})
        
        domain = urlparse(url).netloc # parse the url and extract the domain
        unique_id = str(uuid4()) # create a unique ID. 
  
        settings = {
            'unique_id': unique_id, # unique ID for each record for DB
            'url':url,
            #'USER_AGENT': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
      
        }


Это мой index.html

<form method="POST" class="post-form" id='userinfoform' action="{% url 'crawl' %}" onsubmit="return spinner();">  
        
<div class="container  " >  
<br>  
    <div class="form-group">  
    <label class=" col-form-label"></label>  
    <div class="col-lg">  
    <h3><strong>Please Fill The Details</strong></h3>  
    </div>  
  </div> 
  <br>
  <fieldset class="w-100 border border-rounded p-4"><legend class="w-25 text-center">Domain Form</legend>
    <div class="form-group row">  
    <label class="col-sm-4 col-form-label"><strong>Enter domain name:</strong></label>  
    <div class="col-sm-8">  
        <input type="text" name="id_url" id="id_url" required="" value="">
    </div>  
  </div> <br> 
  
  {% if d %}
  <p style="color: red;">{{d}}</p>
  {% endif %}
  
  
 </fieldset>
    
  
    <div class="form-group row mt-3">  
    <label class="col-sm-4 col-form-label"></label>  
    <div class="col-sm-8 text-right">  
    <button type="submit" class="btn btn-primary w-25" name='submit' >Submit</button>  
    </div>  
    </div>  

    
</div>  
</form> 

Это мой javascript javascript

<script>
    $(document).ready(function(){
    $('#userinfoform').on('submit', function(){
    
    $.ajax({
        method:'POST',
        url:"{% url 'crawl' %}",
        data:{
        id_url:$('#id_url').val(),
        
        
    }, 
    success: function() {
    
    alert('Data Successfully Posted');
        
    },
    });
    
    });
    });
    </script> 

Я привел файлы моего проекта выше. Я просто хочу превратить обычную форму отправки в вызов ajax без потери той же логики.

Вернуться на верх