Django - Link to download CSV in template
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Trying to figure out how to correctly create a download link to a csv file on my template.
Views.py
#this is the view that shows the html page (all queries work)
def admin_view(request, mysite):
context = dict()
context["mysite"] = mysite.upper()
group = whid.lower() + "-group"
user = request.user.get_username()
authorized = is_authorized(user, group)
end_time = timezone.now()
start_time = end_time.replace(day=1, hour=00, minute=00, second=00)
error_message = ("You are not authorized to be in this page.")
context['data'] = CheckIn.objects.filter(Date__range=(start_time, end_time))
today = datetime.datetime.now()
context['today'] = today
context['first_day'] = today.replace(day=1, hour=00, minute=1)
#Calling the csv function here
context['response'] = export_search_csv(start_time, end_time)
if (authorized):
return render(request, 'mykiosk/admin.html', context)
else:
return HttpResponse(error)
#function to grab and create csv
def export_search_csv(start_time, end_time):
data = CheckIn.objects.filter(Date__range=(start_time, end_time))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
Template
<a href=" response">export to csv</a>
Then all I get is a <HttpResponse status_code=200, "text/csv">
when trying to access the link. I'm missing something, I'm just not sure what. Due to internal company regulations, I can't use the djqscsv
library either.
Any help is appreciated.
python django django-models
add a comment |
Trying to figure out how to correctly create a download link to a csv file on my template.
Views.py
#this is the view that shows the html page (all queries work)
def admin_view(request, mysite):
context = dict()
context["mysite"] = mysite.upper()
group = whid.lower() + "-group"
user = request.user.get_username()
authorized = is_authorized(user, group)
end_time = timezone.now()
start_time = end_time.replace(day=1, hour=00, minute=00, second=00)
error_message = ("You are not authorized to be in this page.")
context['data'] = CheckIn.objects.filter(Date__range=(start_time, end_time))
today = datetime.datetime.now()
context['today'] = today
context['first_day'] = today.replace(day=1, hour=00, minute=1)
#Calling the csv function here
context['response'] = export_search_csv(start_time, end_time)
if (authorized):
return render(request, 'mykiosk/admin.html', context)
else:
return HttpResponse(error)
#function to grab and create csv
def export_search_csv(start_time, end_time):
data = CheckIn.objects.filter(Date__range=(start_time, end_time))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
Template
<a href=" response">export to csv</a>
Then all I get is a <HttpResponse status_code=200, "text/csv">
when trying to access the link. I'm missing something, I'm just not sure what. Due to internal company regulations, I can't use the djqscsv
library either.
Any help is appreciated.
python django django-models
add a comment |
Trying to figure out how to correctly create a download link to a csv file on my template.
Views.py
#this is the view that shows the html page (all queries work)
def admin_view(request, mysite):
context = dict()
context["mysite"] = mysite.upper()
group = whid.lower() + "-group"
user = request.user.get_username()
authorized = is_authorized(user, group)
end_time = timezone.now()
start_time = end_time.replace(day=1, hour=00, minute=00, second=00)
error_message = ("You are not authorized to be in this page.")
context['data'] = CheckIn.objects.filter(Date__range=(start_time, end_time))
today = datetime.datetime.now()
context['today'] = today
context['first_day'] = today.replace(day=1, hour=00, minute=1)
#Calling the csv function here
context['response'] = export_search_csv(start_time, end_time)
if (authorized):
return render(request, 'mykiosk/admin.html', context)
else:
return HttpResponse(error)
#function to grab and create csv
def export_search_csv(start_time, end_time):
data = CheckIn.objects.filter(Date__range=(start_time, end_time))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
Template
<a href=" response">export to csv</a>
Then all I get is a <HttpResponse status_code=200, "text/csv">
when trying to access the link. I'm missing something, I'm just not sure what. Due to internal company regulations, I can't use the djqscsv
library either.
Any help is appreciated.
python django django-models
Trying to figure out how to correctly create a download link to a csv file on my template.
Views.py
#this is the view that shows the html page (all queries work)
def admin_view(request, mysite):
context = dict()
context["mysite"] = mysite.upper()
group = whid.lower() + "-group"
user = request.user.get_username()
authorized = is_authorized(user, group)
end_time = timezone.now()
start_time = end_time.replace(day=1, hour=00, minute=00, second=00)
error_message = ("You are not authorized to be in this page.")
context['data'] = CheckIn.objects.filter(Date__range=(start_time, end_time))
today = datetime.datetime.now()
context['today'] = today
context['first_day'] = today.replace(day=1, hour=00, minute=1)
#Calling the csv function here
context['response'] = export_search_csv(start_time, end_time)
if (authorized):
return render(request, 'mykiosk/admin.html', context)
else:
return HttpResponse(error)
#function to grab and create csv
def export_search_csv(start_time, end_time):
data = CheckIn.objects.filter(Date__range=(start_time, end_time))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
Template
<a href=" response">export to csv</a>
Then all I get is a <HttpResponse status_code=200, "text/csv">
when trying to access the link. I'm missing something, I'm just not sure what. Due to internal company regulations, I can't use the djqscsv
library either.
Any help is appreciated.
python django django-models
python django django-models
edited Nov 16 '18 at 14:48
Dan
asked Nov 16 '18 at 14:30
DanDan
616
616
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you are doing it wrong way. Rather than sending it via context, you should convert your export_search_csv
method to a view and use it from template. For example:
def export_search_csv(request, start_date, end_date):
data = CheckIn.objects.filter(Date__range=(datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S"), datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
def admin_view(request, mysite):
...
context['start_date'] = startdate.strftime("%Y-%m-%d %H:%M:%S")
context['end_date'] = enddate.strftime("%Y-%m-%d %H:%M:%S")
...
In template:
<a href="% url 'export_search_csv' start_date end_date %">export to csv</a>
Add new url for new view:
path('your_path/<str:start_date>/<str:end_date>/', export_search_csv, name='export_search_csv'),
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339813%2fdjango-link-to-download-csv-in-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you are doing it wrong way. Rather than sending it via context, you should convert your export_search_csv
method to a view and use it from template. For example:
def export_search_csv(request, start_date, end_date):
data = CheckIn.objects.filter(Date__range=(datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S"), datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
def admin_view(request, mysite):
...
context['start_date'] = startdate.strftime("%Y-%m-%d %H:%M:%S")
context['end_date'] = enddate.strftime("%Y-%m-%d %H:%M:%S")
...
In template:
<a href="% url 'export_search_csv' start_date end_date %">export to csv</a>
Add new url for new view:
path('your_path/<str:start_date>/<str:end_date>/', export_search_csv, name='export_search_csv'),
add a comment |
I think you are doing it wrong way. Rather than sending it via context, you should convert your export_search_csv
method to a view and use it from template. For example:
def export_search_csv(request, start_date, end_date):
data = CheckIn.objects.filter(Date__range=(datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S"), datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
def admin_view(request, mysite):
...
context['start_date'] = startdate.strftime("%Y-%m-%d %H:%M:%S")
context['end_date'] = enddate.strftime("%Y-%m-%d %H:%M:%S")
...
In template:
<a href="% url 'export_search_csv' start_date end_date %">export to csv</a>
Add new url for new view:
path('your_path/<str:start_date>/<str:end_date>/', export_search_csv, name='export_search_csv'),
add a comment |
I think you are doing it wrong way. Rather than sending it via context, you should convert your export_search_csv
method to a view and use it from template. For example:
def export_search_csv(request, start_date, end_date):
data = CheckIn.objects.filter(Date__range=(datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S"), datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
def admin_view(request, mysite):
...
context['start_date'] = startdate.strftime("%Y-%m-%d %H:%M:%S")
context['end_date'] = enddate.strftime("%Y-%m-%d %H:%M:%S")
...
In template:
<a href="% url 'export_search_csv' start_date end_date %">export to csv</a>
Add new url for new view:
path('your_path/<str:start_date>/<str:end_date>/', export_search_csv, name='export_search_csv'),
I think you are doing it wrong way. Rather than sending it via context, you should convert your export_search_csv
method to a view and use it from template. For example:
def export_search_csv(request, start_date, end_date):
data = CheckIn.objects.filter(Date__range=(datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S"), datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
writer = csv.writer(response)
writer.writerow(['id', 'EmpID', 'BarID', 'Username', 'Type', 'Liability', 'Date'])
for item in data:
writer.writerow([item.id, item.EmpID, item.BarID, item.Username, item.Type, item.Liability, item.Date])
return response
def admin_view(request, mysite):
...
context['start_date'] = startdate.strftime("%Y-%m-%d %H:%M:%S")
context['end_date'] = enddate.strftime("%Y-%m-%d %H:%M:%S")
...
In template:
<a href="% url 'export_search_csv' start_date end_date %">export to csv</a>
Add new url for new view:
path('your_path/<str:start_date>/<str:end_date>/', export_search_csv, name='export_search_csv'),
edited Nov 16 '18 at 15:25
answered Nov 16 '18 at 15:16
ruddraruddra
16.7k42951
16.7k42951
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339813%2fdjango-link-to-download-csv-in-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown