The Django framework uses ajax to implement the batch data import function, djangoajax

Source: Internet
Author: User

The Django framework uses ajax to implement the batch data import function, djangoajax

The example in this article shows how to use ajax to import data in batches on the webpage for your reference. The details are as follows:

Url. py code:Copy codeThe Code is as follows: url (R' ^ workimport/$ ', 'keywork. views. import_keywork', name = 'import _ keywork ')
View. py code:

From keywork. models import DevDatafrom django. http import JsonResponse # django ajax part def import_keywork (request): file_sjdr = request. POST ['file _ keywork'] f = open (file_sjdr) WorkList = [] next (f) # Move the file tag to the next line x = y = 0 for line in f: parts = line. replace ('"','') # replace "in the dictionary with null parts = parts. split (',') # Press; to segment the string 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)

For this part of the code, refer 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 + "duplicate data" + data ['failed'] +, successfully imported data "+ data ['success'] +" items ");})}); <form >{% csrf_token %} <label> <I class = "icon-file"> </I> select the file to be imported </label> <input id = "file_keywork" type = "file"/> <input type = "button" id = "btn_sjdr" value = "import" class = "btn-primary btn-sm"/> </form> <div id = "test1"> </div>

The form uses post + ajax. Note that the post method in django must meet two conditions:

Add {% csrf_token %} to form and csrfmiddlewaretoken: "{csrf_token}" to jquery code!

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Read and Write python files and use mysql to insert examples in batches (use python to operate mysql)
  • Python implements batch file name replacement and content replacement
  • Batch cropping of Images Using python
  • Python uses loops to create folders in batches.
  • Python tips: Batch capturing beautiful pictures
  • Python converts the SVG format to png and pdf format for code sharing in batches.
  • Python batch file name modification implementation code
  • How to obtain the file Suffix in python and update the file Suffix in the directory in batches
  • Python Django batch import without repeated data
  • Python Django batch import data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.