Reputation: 23
how can i turn data collected from Django site, and download it as a .csv file. I want it to be so that there is a button in django admin that downloads all the data as a .csv file
this is my model:
from django.db import models
class Auto(models.Model):
YNC = (
('N', 'No'),
('Y', 'Yes'),
)
NYC = (
('N', 'No'),
('Y', 'Yes'),
)
NNY = (
('N', 'No'),
('Y', 'Yes'),
)
nw = (
(1, 'Really Bad'),
(1, 'Bad'),
(3, 'Average'),
(4, 'Good'),
(5, 'Really Good'),
)
Team = models.CharField()
Scout = models.CharField()
StartWithCubeLoaded = models.CharField(max_length = 1, choices = YNC, default = 'N')
CrossAutoLine = models.CharField(max_length = 1, choices = YNC, default = 'N')
RobotCrossCenterLine = models.CharField(max_length = 1, choices = YNC, default = 'N')
PlaceCubeInWrongScale = models.CharField(max_length = 1, choices = YNC, default = 'N')
RobotHitOtherAllianceRobotInNullZone = models.CharField(max_length = 1, choices = YNC, default = 'N')
MisPlaceCube = models.CharField(max_length = 1, choices = YNC, default = 'N')
DroppedCubes = models.IntegerField(default = 0)
DoubleStackScale = models.CharField(max_length = 1, choices = NYC, default = 'N')
Foul = models.CharField(max_length = 1, choices = NYC, default = 'N')
KnockedOffCubes = models.IntegerField(default = 0)
MissedCubes = models.IntegerField(default = 0)
Climbed = models.CharField(max_length=1, choices=NNY, default = 'N')
ClimbTime = models.IntegerField(default=0)
Lifted2Robots = models.CharField(max_length=1, choices=NNY, default = 'N')
ProvidedBar = models.CharField(max_length=1, choices=NNY, default = 'N')
ProvidedRamp = models.CharField(max_length=1, choices=NNY, default = 'N')
GrabFieldBar = models.CharField(max_length=1, choices=NNY, default = 'N')
ClimbAttempted = models.CharField(max_length=1, choices=NNY, default = 'N')
GrabRobotBar = models.CharField(max_length=1, choices=NNY, default = 'N')
ClimbRobotRamp = models.CharField(max_length=1, choices=NNY, default = 'N')
NoShow = models.CharField(max_length=1, choices=NNY, default = 'N')
TippedOver = models.CharField(max_length=1, choices=NNY, default = 'N')
Broke = models.CharField(max_length=1, choices=NNY, default = 'N')
TeamWork = models.IntegerField(choices = nw, default = 'Really Bad')
PickingUpCubes = models.IntegerField(choices = nw, default = 'Really Bad')
Driving = models.IntegerField(choices = nw, default = 'Really Bad')
PlacingCubes = models.IntegerField(choices = nw, default = 'Really Bad')
views.py:
from django.shortcuts import render
from .models import Auto
from .forms import AutoForm
from django.shortcuts import render_to_response, get_object_or_404
def index(modeladmin, request, queryset):
form = AutoForm()
if request.method == "POST":
form = AutoForm(request.POST)
else:
form = AutoForm()
return render(request, 'index.html', {'form': form})
def dcsv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="filename.csv"'
writer = csv.writer(response)
writer.writerow(['column1','column2','column3'])
data = Auto.objects.filter()
for row in data:
rowobj = [row.column1,row.column2,row.column3]
writer.writerow(rowobj)
return response
urls.py:
from django.conf.urls import url
from django.contrib import admin
from system.views import index, dcsv
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', index),
url(r'^$', dcsv),
]
index.html:
<!DOCTYPE html>
<html>
<head>
<!-- UIkit CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.38/css/uikit.min.css" />
<!-- UIkit JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.38/js/uikit.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.38/js/uikit-icons.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Abril+Fatface" rel="stylesheet">
<style>
.uk-logo{
font-family: 'Abril Fatface' !important;
color: black !important;
}
.qf{
font-family: 'Abril Fatface' !important;
color: black !important;
}
</style>
</head>
<body>
<nav class="uk-navbar-container uk-margin uk-navbar-transparent" uk-navbar="mode: click">
<div class="uk-navbar-left">
<a class="uk-navbar-item uk-logo" href="#">Nav</a>
<ul class="uk-navbar-nav">
<li class="uk-active"><a class="qf"href="#">Nav</a></li>
<li><a class="qf" href="#">Nav</a></li>
<li><a class="qf"href="#">Nav</a></li>
</ul>
</div>
</nav>
<h1></h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
</body>
</html>
I found answers on the internet, but they used an older django version. The django version i am using is 1.11.
Upvotes: 1
Views: 1351
Reputation: 3005
Create another view class / function and define that into urls.py and place following code into your new view function / class.
Need to import at top of file
from django.http import HttpResponse
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="filename.csv"'
writer = csv.writer(response)
writer.writerow(['column1','column2','column3'])
data = Auto.objects.filter()
for row in data:
rowobj = [row.column1,row.column2,row.column3]
writer.writerow(rowobj)
return response
AND in html file add link or button and navigate through link or change form action to the new URL using button by calling function. This will start downloading the csv file.
You seems to be very new to django change url line to
url(r'dcsv', dcsv, {},name="dcsv"), instead of url(r'^$', dcsv),
and column1, column2, column3 are just example, replace those with your field names appropriately.
Upvotes: 1