Home Web Front-end JS Tutorial How to import data in batches with ajax

How to import data in batches with ajax

Apr 04, 2018 pm 05:26 PM
ajax import data

This time I will show you how to batch import data with ajax. What are the precautions for ajax batch import of data? The following is a practical case, let's take a look.

The example of this article shares with you the implementation method of using ajax to realize the batch import data function in the web page for your reference. The specific content is as follows

url.py code:

url(r'^workimport/$', 'keywork.views.import_keywork', name='import_keywork')
Copy after login

view.py code:

from keywork.models import DevData
from django.http import JsonResponse #django ajax部分
def import_keywork(request):
 file_sjdr = request.POST['file_keywork']
 f = open(file_sjdr)
 WorkList = []
 next(f) #将文件标记移到下一行
 x = y = 0
 for line in f:
  parts = line.replace('"','') #将字典中的"替换空
  parts = parts.split(',') #按;对字符串进行切片
  if DevData.objects.filter(serv_id = parts[0],user_flag=parts[15]).exists():
   x = x + 1
  else:
   y = y + 1
   WorkList.append(DevData(serv_id=parts[0], serv_state_name=parts[1], acc_nbr=parts[2], user_name=parts[3], acct_code=parts[4], product_id=parts[5],
       mkt_chnl_name=parts[6], mkt_chnl_id=parts[7],mkt_region_name=parts[8], mkt_region_id=parts[9],mkt_grid_name=parts[10],
       sale_man=parts[11],sale_outlets_cd1_name=parts[12], completed_time=parts[13],remove_data=parts[14], user_flag=parts[15],
       pro_flag=parts[16], service_offer_id=parts[17],service_offer_name=parts[18], finish_time=parts[19],staff_name=parts[20],
       staff_code=parts[21],org_name=parts[22],prod_offer_name=parts[23],day_id=parts[24],
       ))
 f.close() 
 DevData.objects.bulk_create(WorkList)
 num = {'success':str(y) ,'fail':str(x) , 'sum':str(x+y)}
 return JsonResponse(num)
Copy after login

This part of the code refers to the previous article (

Django batch import of non-duplicate data)

Code in the template:

$('#btn_sjdr').click(function(){
   $.post("{% url 'import_keywork' %}",
     {
       csrfmiddlewaretoken:"{{ csrf_token }}",
      file_keywork:$("#file_keywork").val(),
     },
     function(data,status) { 
      $("#test1").html(status+"重复数据"+data['fail']+"条,成功导入数据"+data['success']+"条");   
     }           
   )
  });  
<form>
     {% csrf_token %}
     <label><i class="icon-file"></i> 请选择需要被导入的文件</label>
     <input id="file_keywork" type="file"/>
     <input type="button" id="btn_sjdr" value="导入" class="btn btn-primary btn-sm"/>
    </form>
    <p id="test1"></p>
   </p>
Copy after login

The form uses post+ajax. Note that using the post method to submit a form in Django must meet two conditions:

Add {% csrf_token %} to the form, and add csrfmiddlewaretoken:"{ to the

jquery code { csrf_token }}", that's it!

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the use of AJAX's XMLHttpRequest object

##How ajax implements a long connection between the server and the browser

The above is the detailed content of How to import data in batches with ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use ddrescue to recover data on Linux Use ddrescue to recover data on Linux Mar 20, 2024 pm 01:37 PM

Use ddrescue to recover data on Linux

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation!

How to import local songs from NetEase Cloud Music How to import local songs How to import local songs from NetEase Cloud Music How to import local songs Mar 13, 2024 am 11:19 AM

How to import local songs from NetEase Cloud Music How to import local songs

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Slow Cellular Data Internet Speeds on iPhone: Fixes

The first robot to autonomously complete human tasks appears, with five fingers that are flexible and fast, and large models support virtual space training The first robot to autonomously complete human tasks appears, with five fingers that are flexible and fast, and large models support virtual space training Mar 11, 2024 pm 12:10 PM

The first robot to autonomously complete human tasks appears, with five fingers that are flexible and fast, and large models support virtual space training

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times.

See all articles