How to Upload Any Csv Files in Django

In this article nosotros volition discuss how to upload a csv file and and so process the content without storing file on server.

Ane approach could exist uploading the file, storing it in upload directory and and then reading the file.

Another approach could be uploading file and reading it directly from post data without storing it in memory and displaying the information.

We volition work with the later arroyo here.

Uploading CSV file:

First create HTML form to upload the csv file. Use below code for the same.

<form action="{% url "myapp:upload_csv" %}" method="POST" enctype="multipart/form-data" class="grade-horizontal">  {% csrf_token %} <div class="form-group">     <label for="name" class="col-md-3 col-sm-3 col-xs-12 command-characterization">File: </label>     <div form="col-md-8">         <input blazon="file" proper noun="csv_file" id="csv_file" required="True" class="form-control">     </div>                     </div> <div class="form-group">                         <div class="col-md-three col-sm-3 col-xs-12 col-md-offset-3" fashion="margin-bottom:10px;">          <button class="btn btn-main"> <span course="glyphicon glyphicon-upload" way="margin-correct:5px;"></span>Upload </push button>     </div>  </div> </form>

Of import: Exercise not forget to include enctype="multipart/form-data" in class. upload csv in django.png

Add a URL in URLpatterns.

url(r'^upload/csv/$', views.upload_csv, name='upload_csv'),

Create a function in views with the name upload_csv .

Process the CSV file:

In view function, get the file from post information and process information technology. I used below code in my project.

def upload_csv(asking): 	information = {} 	if "Go" == request.method: 		render render(asking, "myapp/upload_csv.html", information)     # if not GET, then go on 	try: 		csv_file = asking.FILES["csv_file"] 		if not csv_file.name.endswith('.csv'): 			letters.error(asking,'File is non CSV type') 			return HttpResponseRedirect(reverse("myapp:upload_csv"))         #if file is too large, return 		if csv_file.multiple_chunks(): 			messages.error(request,"Uploaded file is too big (%.2f MB)." % (csv_file.size/(one thousand*1000),)) 			return HttpResponseRedirect(reverse("myapp:upload_csv"))  		file_data = csv_file.read().decode("utf-8")		  		lines = file_data.split("\n") 		#loop over the lines and save them in db. If error , store as cord and so display 		for line in lines:						 			fields = line.split(",") 			data_dict = {} 			data_dict["name"] = fields[0] 			data_dict["start_date_time"] = fields[1] 			data_dict["end_date_time"] = fields[2] 			data_dict["notes"] = fields[3] 			attempt: 				form = EventsForm(data_dict) 				if form.is_valid(): 					form.relieve()					 				else: 					logging.getLogger("error_logger").error(grade.errors.as_json())												 			except Exception as e: 				logging.getLogger("error_logger").error(repr(e))					 				laissez passer  	except Exception equally east: 		logging.getLogger("error_logger").error("Unable to upload file. "+repr(east)) 		messages.error(request,"Unable to upload file. "+repr(e))  	render HttpResponseRedirect(reverse("myapp:upload_csv"))

In the to a higher place code we are performing below actions:

      - If this is a GET request then return the upload csv html file.

      - If this is a POST request then proceed.

      - First bank check if file name is non catastrophe with .csv then this is not the valid file. You may implement you own checks every bit well.

      - And then we check if file is too large. If these tests fail, we return to html form folio with appropriate fault message. For displaying error/success messages, we are using letters framework. Please import required modules.

      - So we read the file and split the content by new line character.

      - Iterate over each line and split the line using comma.

      - We are assuming that our csv file take 4 columns of data. Nosotros stored the data in a dictionary and then pass the data dictionary to a form.

      - If form is valid and then we go along to relieve the form and hence creating entry in DB.

      - If class is non valid, or any other error is thrown then nosotros log the error in log file.

      Read here about logging the errors in log files. This might exist useful on live servers where debug is fix to imitation.

        Please provide your inputs.

        Adding Robots.txt file to Django Application

        Adding robots.txt file in your Django awarding, Easiest way to add robots.txt file in Django, Django awarding robots.txt file, Why should you add together robots.txt file in your Django Awarding,...

        How to upgrade to paid account on PythonAnyWhere

        Which is the best server for hosting Django Apps, Best hosting provider for Django Apps, Cheapest Django Hosting, PythonAnyWhere Reviews, Django Hosting,...

        Server Access Logging in Django using middleware

        Creating access logs in Django application, Logging using middleware in Django app, Creating custom middleware in Django, Server access logging in Django, Server Access Logging in Django using middleware...

        Improve Your Python Practices: Debugging, Testing, and Maintenance

        improving your python skills, debugging, testing and practice, pypi...

        adamssuarry.blogspot.com

        Source: https://pythoncircle.com/post/30/how-to-upload-and-process-the-csv-file-in-django/

        0 Response to "How to Upload Any Csv Files in Django"

        Post a Comment

        Iklan Atas Artikel

        Iklan Tengah Artikel 1

        Iklan Tengah Artikel 2

        Iklan Bawah Artikel