Create CSV with DjangoDjango uses python's built-in csv library to create Dynamic CSV (Comma Separated values) file. We can use this library into our project's view file. Lets see an example, here we have a django project to that we are implementing this feature. A view function getfile() is created. Django CSV ExampleIn this example, we are creating CSV using static data. // Views.py // urls.py Provide url for the function. While executing to the browser, it renders a CSV file. See the example. Apart from static data, we can get CSV from database too. See, the following example in which we are getting data from the table by using the Employee model. Dynamic CSV using Database// views.py from myapp.models import Employee import csv Output: Save the file and open into the text editor that contains the following data. This data is retrieve from the table employee, a snapshot of table is shown below. Well, we have seen that this library is very useful to create dynamic CSV file. Now, implement it into django project when required.
Next TopicDjango Admin
|