These are chat archives for django/django

10th
Apr 2017
stunpop
@stunpop
Apr 10 2017 07:16
Seems that JS is a much more active and helpful community. Weird since I started learning Python Bec I'd heard that Python users were super active, ATM that's not seeming the case sadly
John
@flyboy1565
Apr 10 2017 07:44
@stunpop You could also join the python room or djangogirls tutorial
Ben Chadwick
@BenCh94
Apr 10 2017 09:32
Hi, I have a Django web app which allows users to download a CSV report of data in a MongoDB. Locally this feature works perfectly using Http response. For some reason on Azure the site works except for the CSV method. I'm getting a 404 error on the URL. Any ideas why this might be happening. My guess is I'll need to reconfigure something in settings.py for Azure.
stunpop
@stunpop
Apr 10 2017 11:22
@flyboy1565 I've been skimming through it and seems mostly very advanced stuff
@flyboy1565 unless I'm in the wrong one!? Lol you have a link?
Amrit Kumar Sharma
@AmritKrsh
Apr 10 2017 12:30
hi.. anyone knows mongoengine
MBijman
@MBijman
Apr 10 2017 13:14
@BenCh94 Can you show show the View function or class handling that csv download
What is your stack running in? Can you ping the mongodb from the machine django is running?
Ben Chadwick
@BenCh94
Apr 10 2017 13:22
The connection to the database is working fine. The user can select a date range and the view renders a page displaying data for that date range. Then they have the option to download that data in a csv file. In the view if the request.method == "POST" the page with data is returned then the part of the view for generating the csv is as follows:
             elif request.method == 'GET':
                  if 'emp-csv' in request.GET:
                    # data from date selection
                    data = request.GET['data']
                    # Convert string to list
                    cleandata = ast.literal_eval(data)
                    # creating csv response
                    response = HttpResponse(content_type="text/csv")
                    response['Content-Disposition'] = "attachment; filename='hours_report.csv'"
                    # Write data to csv file and return
                    fieldnames = ['Supervisor', 'EmployeeID', 'FirstName', 'LastName', 'NT', 'OT', 'OT2', 'Project', 'Client', 'date']
                    writer = csv.DictWriter(response, fieldnames=fieldnames, dialect='excel', )
                    writer.writeheader()
                    writer.writerows(cleandata)

                    return response
@MBijman I posted a question on Stackoverflow with a bit more detail
MBijman
@MBijman
Apr 10 2017 13:31
so the most important thing in web programming is allowing for failures
'The connection to the database is working fine.' can you test this assumption
what does the data get parameter of the request contains?
MBijman
@MBijman
Apr 10 2017 13:38
writer = csv.DictWriter(response, fieldnames=fieldnames, dialect='excel', ) I think you want to write to buffer
import io

buffer = io.StringIO()
wr = csv.writer(buffer, quoting=csv.QUOTE_ALL)
wr.writerows(file_rows)

buffer.seek(0)
response = HttpResponse(buffer, content_type='text/csv')
response['Content-Disposition'] = 'attachment; 

return response
Ben Chadwick
@BenCh94
Apr 10 2017 13:41
the data parameter contains an array of objects
MBijman
@MBijman
Apr 10 2017 13:42
@BenCh94 ast.literal_eval(data) is user input, as such you should see it as unsafe use json.loads() instead
storing that in url is not good idea, put it inside a cookie
I think that was the issue
you had some fluf in the url and the your regex's did not match
Ben Chadwick
@BenCh94
Apr 10 2017 13:45
@MBijman I'll implement that change now and let you know, thanks for the help!!
MBijman
@MBijman
Apr 10 2017 13:46
no problem keep me posted
Ben Chadwick
@BenCh94
Apr 10 2017 15:11
@MBijman That worked nicely. I ended up separating the csv export into a new view, much neater than it was before. Thanks for the help!
Miracle Ayodele
@iammiracle
Apr 10 2017 15:20
hi guys
i am having issues with timezone
and its really affecting my database
Cristian Samaniego
@chrisRubiano
Apr 10 2017 18:33
is there a way to get the previous and next item from a django model?
Philip Eckert
@pae4557
Apr 10 2017 20:21
anybody ever had an issue with openssl being outdated on mac when running a test server?
Em
@emTr0
Apr 10 2017 21:39
Does anyone have a blog with their opinion on using bootstrap with DjangoCMS, Mezzanine or Wagtail?