Use a Django form to add a Web page to an upload file and analyze the file.

Source: Internet
Author: User

The development environment is:

Apache + python + django+ Eclipse (development environment)

To achieve the purpose:

On the Web page, add the upload file control. Then read the CSV file and parse the CSV file.

Operation Steps:

Django is the design pattern of MVC (model M, view V, controller C)

1. Build your own model from Django's basic model.

Add the following code to the myforms.py file, and the data for this model can be used in the HTML-displayed form data.

 fromDjangoImportForms fromDjango.views.decorators.csrfImportcsrf_exempt@csrf_exemptclassBatchoptform (forms. Form): Stype= Forms. Choicefield (choices= (1,'type1'), (2,'type2'), (3,'Type3'), (4,'Type4'), (5,'Type5'), (6,'Type6'), label="Batchtype") Batchfile= Forms. Filefield (Required=true, label="Data Files")

2. In view class viewsql.py, complete the call to the form.

defdosomething (Request): Data=_getreqdata (Request, Sys._getframe (0). F_code.co_name)if 'Whitelist'! = Data.get ('opt'): Batchform=Batchoptform (Request. POST, request. FILES) #调用模型ifbatchform.is_valid (): batchform.fields['Stype'] = forms. Choicefield (choices= (1,''), (2,'Hand Q'), label='bulk White list type')           batchform.fields['Batchfile'] = forms. Filefield (Required=true, label=None)returnRender_to_response (whitelist.html, {'form': Batchform}) #将表单render to the Web page

3, through urls.py this unified resources to manage resources

URL (r'^whitelist/$'protocoltool.views.whitelist', name= ' Whitelist '),

4. Paste the form label in the whitelist.html

<table id=""Border='0'Bordercolor="#FFFFFF"Width='98%'cellpadding='0'cellspacing='0'align='Center'Bgcolor="'> <tbody> <tr style="Vertical-align:middle"> &LT;TD style="width:45%; "> {Form.stype.label}} </td>&LT;TD style="width:50%;">{{Form.stype}}</td> </tr> <tr style="Vertical-align:middle"&LT;TD style="width:20%">{{Form.batchfile.label}}</td> &LT;TD style="width:30%">{{Form.batchfile}}</td> <button style="width:80px;" class="btn Btn-primary"data-loading-text="Import in ..."Id='IMPORT_WL'onclick='Importbatch ()'; Type="Button"> Bulk Import </button></td> </tr> </tbody></table>

5, in the script cncsql.js file, the corresponding Importbatch ()

function Importbatch () {var fname= $('#id_batchfile'). Val (). Trim (); # Auto-generatedif(""==fname) {Alert ("No batch account opening file selected"); return; }$('#IMPORT_WL'). Button ("Loading");var fdata=new FormData (); #构建表单数据 Fdata.append ('opt','Whitelist');Fdata.append ('Stype', $('#id_stype'). Val ()); #自动生成的 Fdata.append ('Batchfile', $('#id_batchfile') [0].files[0]); $.ajax ({URL:'/whitelist/', Data:fdata, Cache:false, Contenttype:false, Processdata:false, type: 'POST', Success:function (rdata) {$ ('#IMPORT_WL'). Button ("Reset");Rdata=Json.parse (rdata); .........
} });}

6. Get

7, through batchform.cleaned_data[' Batchfile '] to invoke and process the relevant data

CSVFile =batchform.cleaned_data['batchfile'  for in Csvfile.read (). Split (LINESEP):        = line.strip ()        ...

This completes the modeling, view composition, and data processing of the form.

Use a Django form to add a Web page to an upload file and analyze the file.

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.