Re: Link to download a file

2017-09-07 Thread Andréas Kühne
Hi,

I would recommend that you checkout how django uses storage backends. The
reason for that is that if you would like to use your application with a
cloud provider that has a storage solution - for example S3, your code
won't work the way it is written now.

However if you just make some minor changes - it will work directly without
any major problems.

Just do this:

view.py

from django.core.files.storage import default_storage

def download(request):
data = default_storage.open('static/tmp/dati.csv').read()
resp = HttpResponse(data, mimetype='application/x-download')
resp['Content-Disposition'] = 'attachment; filename=dati.csv'
return resp

Like I said, the reason for this is that you in the case don't have to
handle the MEDIA_ROOT settings yourself and it all works the same anyway.
Should you decide to move to a cloud provider that has some sort of storage
- you can then use the django-storages package and easily connect it,
without changing your code.

Regards,

Andréas

2017-09-07 10:33 GMT+02:00 giuseppe ricci <peppepega...@gmail.com>:

> Thanks.
> My solution:
>
> urls.py
> url(r'^download/$', 'views.download', name='download'),
>
> template.html
> Download dei dati href="home">Indietro
>
> views.py
> def download(request):
> data = open(os.path.join(settings.MEDIA_ROOT, 'static/tmp/dati.csv'),
> 'r').read()
> resp = HttpResponse(data, mimetype='application/x-download')
> resp['Content-Disposition'] = 'attachment; filename=dati.csv'
> return resp
> it works!
>
>
> Il giorno giovedì 31 agosto 2017 17:58:27 UTC+2, giuseppe ricci ha scritto:
>
>> Hi guys, I need some help to insert a link to download a file. In a view
>> I read data from a csv file and I show them in a table in a template. Under
>> the table I need to insert, when there are data,
>> a link similar as Download data and when user click it, he download the
>> datafile, previously I wrote the same data in a csv file. You can see a
>> part of interested view and the template to show data.
>> In the code doesn't download anything.
>>
>> views.py
>>
>> listafile = os.listdir('static/dati')
>>
>> fileok=[]
>> context={}
>> i=0
>> for file in listafile:
>> inizio=str(file[0:4]).strip()
>> if inizio==anno:
>> fileok.append(file)
>> elif inizio=='coun':
>> contaf=str(file[6:10]).strip()
>> if contaf==anno:
>> fileok.append(file)
>> else:
>> pass #print "File NON trovato", inizio, anno
>>
>> for nomefile in fileok:
>> nomedato=str(nomefile[5:-4]).strip()
>> if nomedato==tipodati:
>> path2file=str(("static/dati/"+str(nomefile)).strip())
>> with open(path2file) as f:
>> for line in f:
>> riga = line.split(";")
>> if riga[0]==cciaa:
>> context[i]=line
>> i=i+1
>> #print "context", context
>>
>> d2 = {  k:v.split(";") for k,v in context.items() }
>> j=0
>> datilist={}
>> field_names = ['Provincia CCIAA', 'Data Ora', 'Numero REA',
>> 'Forma Giuridica', 'Count dati']
>> f=open('static/tmp/dati.csv', 'w')
>> writer = csv.writer(f) #questa definisce la variabile writer
>> usata x scrivere i dati nel file csv!!!
>> writer.writerow(field_names)
>>
>> while j<len(d2):
>> datilist[j]=[d2[j][0], d2[j][3], d2[j][5], d2[j][7],
>> d2[j][10].rstrip(), ''] #
>> writer.writerow(datilist[j])
>> j=j+1
>>
>> return render_to_response('showdata.html',
>> context_instance=RequestContext(request, {'dictdati': datilist} ))
>>
>> else:
>>
>> return render_to_response('home.html',
>> context_instance=RequestContext(request, {'var': d} ))
>>
>>
>> def showdata(request):
>> return render_to_response('showdata.html',
>> context_instance=RequestContext(request, context))
>>
>> showdata.html
>> 
>> 
>> Statistiche Dkey
>> Ecco in forma tabellare i dati richiesti..
>>
>> 
>>
>> 
>>   table { width: 90%; background-color: #FF; color:
>> #00; }
>>   th, td { width: 4%; }
>> 
>>
>>  

Re: Link to download a file

2017-09-07 Thread giuseppe ricci
Thanks.
My solution:

urls.py
url(r'^download/$', 'views.download', name='download'),

template.html
Download dei datiIndietro 

views.py
def download(request):
data = open(os.path.join(settings.MEDIA_ROOT, 'static/tmp/dati.csv'), 
'r').read()
resp = HttpResponse(data, mimetype='application/x-download')
resp['Content-Disposition'] = 'attachment; filename=dati.csv'
return resp
it works!


Il giorno giovedì 31 agosto 2017 17:58:27 UTC+2, giuseppe ricci ha scritto:
>
> Hi guys, I need some help to insert a link to download a file. In a view I 
> read data from a csv file and I show them in a table in a template. Under 
> the table I need to insert, when there are data, 
> a link similar as Download data and when user click it, he download the 
> datafile, previously I wrote the same data in a csv file. You can see a 
> part of interested view and the template to show data. 
> In the code doesn't download anything.
>
> views.py 
>
> listafile = os.listdir('static/dati')
>
> fileok=[]
> context={}
> i=0
> for file in listafile:
> inizio=str(file[0:4]).strip()
> if inizio==anno:   
> fileok.append(file)
> elif inizio=='coun':
> contaf=str(file[6:10]).strip()
> if contaf==anno:
> fileok.append(file)
> else:
> pass #print "File NON trovato", inizio, anno
> 
> for nomefile in fileok:
> nomedato=str(nomefile[5:-4]).strip()
> if nomedato==tipodati:
> path2file=str(("static/dati/"+str(nomefile)).strip())
> with open(path2file) as f:
> for line in f:
> riga = line.split(";")
> if riga[0]==cciaa:
> context[i]=line
> i=i+1
> #print "context", context
>
> d2 = {  k:v.split(";") for k,v in context.items() }
> j=0
> datilist={} 
> field_names = ['Provincia CCIAA', 'Data Ora', 'Numero REA', 'Forma 
> Giuridica', 'Count dati']
> f=open('static/tmp/dati.csv', 'w') 
> writer = csv.writer(f) #questa definisce la variabile writer usata 
> x scrivere i dati nel file csv!!!
> writer.writerow(field_names)
>
> while j<len(d2):
> datilist[j]=[d2[j][0], d2[j][3], d2[j][5], d2[j][7], 
> d2[j][10].rstrip(), ''] #
> writer.writerow(datilist[j])
> j=j+1  
> 
> return render_to_response('showdata.html', 
> context_instance=RequestContext(request, {'dictdati': datilist} )) 
> 
> else:
>
> return render_to_response('home.html', 
> context_instance=RequestContext(request, {'var': d} ))
>
>
> def showdata(request):
> return render_to_response('showdata.html', 
> context_instance=RequestContext(request, context))
>
> showdata.html
> 
> 
> Statistiche Dkey
> Ecco in forma tabellare i dati richiesti..
>   
> 
>
> 
>   table { width: 90%; background-color: #FF; color: 
> #00; }
>   th, td { width: 4%; }
> 
> 
>align="center">
> 
> 
>   Nome
>   Data_Ora
>   Numero
>   Forma
>   Count
>  
> 
> 
> 
>  
>  
>   
>   {% for key, item in dictdati.items %}   
>  
>   {% for idx in item %} 
> {% if idx != '' %} 
>   {{ idx }}
> {% else %}
> 
> {% endif %}
>   {% endfor %}  
>  
>   {% endfor %}
> 
> 
>   
> 
>
> Download dei dati
>
> 
>  
>
>
>
>
> Can someone give me some suggestion? Other posts didn't aided me. 
> Thanks.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1fca0b1e-a952-4694-9bdb-177f1ac6b69c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download a file

2017-09-06 Thread James Schneider
On Wed, Sep 6, 2017 at 6:03 AM, giuseppe ricci 
wrote:

> Thank you James for your reply..
> So in my urls.py I insert a new entry:
>
> url(r'^download/$', 'views.download')
>
> and my view to download a file is:
>
> def download(request):
> file_path = 'static/tmp/' #settings.MEDIA_ROOT
> return render_to_response('download.html', file_path+'dati.csv')
>
>


Your view should look similar to what the Django docs show here:

https://docs.djangoproject.com/en/1.11/howto/outputting-csv/

Is this a file that is publicly available? If so, it may be more efficient
to have the web server process handle the download directly rather than
Django. That would require a working setup for your STATIC_DIR and
MEDIA_DIR.

If that's the case, simply add a link to the file URL directly, something
like this in your template:

Download

Where {{ file_path }} is a path to the file in your media or static files
(if the file changes, use the media directory, if it stays the same, use
the static directory). Your web server should be configured to serve your
MEDIA_URL and STATIC_URL already.

This site appears to have several approaches detailed:
http://voorloopnul.com/blog/serving-large-and-small-files-with-django/

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciV9TO6q3W5EzAKGvhv7nTeeg_XTjmYXWS0QMMUzB5qoBw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download a file

2017-09-06 Thread giuseppe ricci
Thank you James for your reply..
So in my urls.py I insert a new entry:

url(r'^download/$', 'views.download')

and my view to download a file is:

def download(request):
file_path = 'static/tmp/' #settings.MEDIA_ROOT
return render_to_response('download.html', file_path+'dati.csv')

and the download.html is a simple page with a write Download file ok..
But in this manner I receive the error 

TemplateDoesNotExist at /download/

static/tmp/dati.csv


Can you help me?

Thanks.



Il giorno giovedì 31 agosto 2017 17:58:27 UTC+2, giuseppe ricci ha scritto:
>
> Hi guys, I need some help to insert a link to download a file. In a view I 
> read data from a csv file and I show them in a table in a template. Under 
> the table I need to insert, when there are data, 
> a link similar as Download data and when user click it, he download the 
> datafile, previously I wrote the same data in a csv file. You can see a 
> part of interested view and the template to show data. 
> In the code doesn't download anything.
>
> views.py 
>
> listafile = os.listdir('static/dati')
>
> fileok=[]
> context={}
> i=0
> for file in listafile:
> inizio=str(file[0:4]).strip()
> if inizio==anno:   
> fileok.append(file)
> elif inizio=='coun':
> contaf=str(file[6:10]).strip()
> if contaf==anno:
> fileok.append(file)
> else:
> pass #print "File NON trovato", inizio, anno
> 
> for nomefile in fileok:
> nomedato=str(nomefile[5:-4]).strip()
> if nomedato==tipodati:
> path2file=str(("static/dati/"+str(nomefile)).strip())
> with open(path2file) as f:
> for line in f:
> riga = line.split(";")
> if riga[0]==cciaa:
> context[i]=line
> i=i+1
> #print "context", context
>
> d2 = {  k:v.split(";") for k,v in context.items() }
> j=0
> datilist={} 
> field_names = ['Provincia CCIAA', 'Data Ora', 'Numero REA', 'Forma 
> Giuridica', 'Count dati']
> f=open('static/tmp/dati.csv', 'w') 
> writer = csv.writer(f) #questa definisce la variabile writer usata 
> x scrivere i dati nel file csv!!!
> writer.writerow(field_names)
>
> while j<len(d2):
> datilist[j]=[d2[j][0], d2[j][3], d2[j][5], d2[j][7], 
> d2[j][10].rstrip(), ''] #
> writer.writerow(datilist[j])
> j=j+1  
> 
> return render_to_response('showdata.html', 
> context_instance=RequestContext(request, {'dictdati': datilist} )) 
> 
> else:
>
> return render_to_response('home.html', 
> context_instance=RequestContext(request, {'var': d} ))
>
>
> def showdata(request):
> return render_to_response('showdata.html', 
> context_instance=RequestContext(request, context))
>
> showdata.html
> 
> 
> Statistiche Dkey
> Ecco in forma tabellare i dati richiesti..
>   
> 
>
> 
>   table { width: 90%; background-color: #FF; color: 
> #00; }
>   th, td { width: 4%; }
> 
> 
>align="center">
> 
> 
>   Nome
>   Data_Ora
>   Numero
>   Forma
>   Count
>  
> 
> 
> 
>  
>  
>   
>   {% for key, item in dictdati.items %}   
>  
>   {% for idx in item %} 
> {% if idx != '' %} 
>   {{ idx }}
> {% else %}
> 
> {% endif %}
>   {% endfor %}  
>  
>   {% endfor %}
> 
> 
>   
> 
>
> Download dei dati
>
> 
>  
>
>
>
>
> Can someone give me some suggestion? Other posts didn't aided me. 
> Thanks.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/11d66199-3718-4117-a7ec-bc4163676b63%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download a file

2017-09-01 Thread James Schneider
On Aug 31, 2017 8:57 AM, "giuseppe ricci" <peppepega...@gmail.com> wrote:

Hi guys, I need some help to insert a link to download a file. In a view I
read data from a csv file and I show them in a table in a template. Under
the table I need to insert, when there are data,
a link similar as Download data and when user click it, he download the
datafile, previously I wrote the same data in a csv file. You can see a
part of interested view and the template to show data.
In the code doesn't download anything.


There's a few ways to handle this.

1. Your 'download' button could simply be a link back to the same page with
an additional GET argument (ie ?download= ). In your view, you would
generate the data dict normally. Before you start returning responses
though, you would check for the presence of the 'download' GET argument via
request.GET.get('download', False). If you get anything but False (meaning
they clicked the link), you can tweak the response headers and send back
only the data you've gathered:

https://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

Depending on what kind of file you are returning, you may need to create an
in memory file using StringIO or BytesIO and return that.

If you want to have a separate view handle the file download, simply add
something like '/download/' to the end of the URL as another urls.py entry,
and assume that the user wants to download the file within the view you
specify for your URL. If you do that, I suggest you break out the CSV
reading/writing logic (along with and file creation work) in to separate
functions from the view so that both views access the data in the same way,
they simply render and return it differently.

Another way would be to accept a POST submission through a form and then
return the file as I explained above.

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVUz1nG%2BWw%2BHAr2wSLh9DuK-9xnY5CKkbbGpbh4TPLiMg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Link to download a file

2017-08-31 Thread giuseppe ricci
Hi guys, I need some help to insert a link to download a file. In a view I 
read data from a csv file and I show them in a table in a template. Under 
the table I need to insert, when there are data, 
a link similar as Download data and when user click it, he download the 
datafile, previously I wrote the same data in a csv file. You can see a 
part of interested view and the template to show data. 
In the code doesn't download anything.

views.py 

listafile = os.listdir('static/dati')

fileok=[]
context={}
i=0
for file in listafile:
inizio=str(file[0:4]).strip()
if inizio==anno:   
fileok.append(file)
elif inizio=='coun':
contaf=str(file[6:10]).strip()
if contaf==anno:
fileok.append(file)
else:
pass #print "File NON trovato", inizio, anno

for nomefile in fileok:
nomedato=str(nomefile[5:-4]).strip()
if nomedato==tipodati:
path2file=str(("static/dati/"+str(nomefile)).strip())
with open(path2file) as f:
for line in f:
riga = line.split(";")
if riga[0]==cciaa:
context[i]=line
i=i+1
#print "context", context

d2 = {  k:v.split(";") for k,v in context.items() }
j=0
datilist={} 
field_names = ['Provincia CCIAA', 'Data Ora', 'Numero REA', 'Forma 
Giuridica', 'Count dati']
f=open('static/tmp/dati.csv', 'w') 
writer = csv.writer(f) #questa definisce la variabile writer usata 
x scrivere i dati nel file csv!!!
writer.writerow(field_names)

while j<len(d2):
datilist[j]=[d2[j][0], d2[j][3], d2[j][5], d2[j][7], 
d2[j][10].rstrip(), ''] #
writer.writerow(datilist[j])
j=j+1  

return render_to_response('showdata.html', 
context_instance=RequestContext(request, {'dictdati': datilist} )) 

else:

return render_to_response('home.html', 
context_instance=RequestContext(request, {'var': d} ))


def showdata(request):
return render_to_response('showdata.html', 
context_instance=RequestContext(request, context))

showdata.html


Statistiche Dkey
Ecco in forma tabellare i dati richiesti..
  



  table { width: 90%; background-color: #FF; color: 
#00; }
  th, td { width: 4%; }


  


  Nome
  Data_Ora
  Numero
  Forma
  Count
 



 
 
  
  {% for key, item in dictdati.items %}   
 
  {% for idx in item %} 
{% if idx != '' %} 
  {{ idx }}
{% else %}

{% endif %}
  {% endfor %}  
 
  {% endfor %}


  


Download dei dati


 




Can someone give me some suggestion? Other posts didn't aided me. 
Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3e7a5919-f176-4789-a4dc-158e3bc906ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download

2017-05-05 Thread 'Abraham Varricatt' via Django users
That's an interesting project!

If I use WhiteNoise as part of a production deploy somewhere, can I skip 
(or ignore) running 'manage.py collectstatic' ? 

Yours,
Abraham V.


On Wednesday, 3 May 2017 15:13:36 UTC-4, Dan Tagg wrote:
>
> You might want to check out WhiteNoise (
> https://whitenoise.readthedocs.io/en/stable/) perhaps in conjunction with 
> a CDN
>
> On 3 May 2017 at 19:35, Tim Chase  > wrote:
>
>> On 2017-05-02 19:11, Antonis Christofides wrote:
>> > response = HttpResponse(csvfile.read(), content_type='text/csv')
>>
>> Beware that, if your content can get huge (some of our reports can
>> run to hundreds of megabytes), you might want to investigate
>> something like the "sendfile" alternatives or spew it out as generator
>> instead of doing .read() to suck the whole thing into memory.
>>
>> -tkc
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com .
>> To post to this group, send email to django...@googlegroups.com 
>> .
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/20170503133519.05365d67%40bigbox.christie.dr
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Wildman and Herring Limited, Registered Office: Sir Robert Peel House, 178 
> Bishopsgate, London, United Kingdom, EC2M 4NJ, Company no: 05766374
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/18c9354a-0325-4e53-ad4c-2bda5e7a2877%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download

2017-05-03 Thread Dan Tagg
You might want to check out WhiteNoise (
https://whitenoise.readthedocs.io/en/stable/) perhaps in conjunction with a
CDN

On 3 May 2017 at 19:35, Tim Chase  wrote:

> On 2017-05-02 19:11, Antonis Christofides wrote:
> > response = HttpResponse(csvfile.read(), content_type='text/csv')
>
> Beware that, if your content can get huge (some of our reports can
> run to hundreds of megabytes), you might want to investigate
> something like the "sendfile" alternatives or spew it out as generator
> instead of doing .read() to suck the whole thing into memory.
>
> -tkc
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/20170503133519.05365d67%40bigbox.christie.dr.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Wildman and Herring Limited, Registered Office: Sir Robert Peel House, 178
Bishopsgate, London, United Kingdom, EC2M 4NJ, Company no: 05766374

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPZHCY5FnmudhJSV5k2FBptiK1EdGk1%3D7vhyXicbJZf1uD3j3w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download

2017-05-03 Thread Tim Chase
On 2017-05-02 19:11, Antonis Christofides wrote:
> response = HttpResponse(csvfile.read(), content_type='text/csv')

Beware that, if your content can get huge (some of our reports can
run to hundreds of megabytes), you might want to investigate
something like the "sendfile" alternatives or spew it out as generator
instead of doing .read() to suck the whole thing into memory.

-tkc



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20170503133519.05365d67%40bigbox.christie.dr.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download

2017-05-03 Thread Sixtine Vernhes
It's work with the use of media :) 
Thanks for the help ! 

Le mardi 2 mai 2017 18:13:01 UTC+2, Antonis Christofides a écrit :
>
> You cannot get a file outside of your project unless you symlink it inside 
> the project. This is also a Very Bad Thing(TM) as it may allow attackers to 
> request arbitrary files.
>
> You *can* get a file outside your project. Whether this is a bad thing or 
> not depends on why and how. I also don't think this file should be placed 
> in the static directory. The static directory is for files that don't 
> change after the program is installed. The media directory might be more 
> appropriate.
>
> If what you are doing is to dynamically create a csv file for your user to 
> download immediately, one possible way of doing that is (untested):
>
> from tempfile import TemporaryFile
> csvfile = TemporaryFile()
> export_your_data_to_the_file(csvfile)
> file_size = csvfile.tell()
> csvfile.seek(0)
> response = HttpResponse(csvfile.read(), content_type='text/csv')
> response['Content-Disposition'] = 'attachment; filename=whateveryoulike.csv'
> response['Content-Length'] = str(size)
> return response
>
> Regards,
>
> A.
>
> Antonis Christofideshttp://djangodeployment.com
>
> On 2017-05-02 17:53, m712 - Developer wrote:
>
> You cannot get a file outside of your project unless you symlink it inside 
> the project. This is also a Very Bad Thing(TM) as it may allow attackers to 
> request arbitrary files.
> What you should do instead is this:
> 1) Put Data/01/export.txt to the static/ folder inside your app (with the 
> same folder structure if you need it that way)
> 2) Build the path to your file with /static/ as prefix, i.e. 
> http://127.0.0.1/static/Data/01/export.txt
> On May 2, 2017 3:44 PM, Sixtine Vernhes   
> wrote:
>
> This is in development. I try to send url of my file in views.py : 
>
>  return render(request, "export.html", {'out': urlOut})
>
> and in my template I have the link : 
>
> Lien du  fichier 
>
> but when I open it I have this link : 
>
> http://127.0.0.1:8000/home/myuser/Data/01/export.txt 
>
> and I want to have this to download the file : 
> /home/myuser/Data/01/export.txt 
>
> Have you any ideas? 
>
> (sorry i'm newbie in django ^^)
>
> Le mardi 2 mai 2017 14:04:05 UTC+2, Antonis Christofides a écrit : 
>>
>> Is this in production or development? What is the url that doesn't work? 
>> What happens when you try the url?
>>
>> Regards,
>>
>> A.
>>
>> Antonis Christofideshttp://djangodeployment.com
>>
>> On 2017-05-02 11:28, Sixtine Vernhes wrote:
>>
>> Hi ! 
>>
>> I try to create a link on Django who download a static csv file, but I 
>> have no idea how to do. 
>> In my settings.py :
>>
>> STATIC_URL = '/static/'
>> STATICFILES_DIRS = (
>> os.path.join(BASE_DIR, "static/"),
>> )
>>
>> and my file is in this directory 
>>
>> Would anyone have an idea ? 
>>
>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com .
>> To post to this group, send email to django...@googlegroups.com 
>> .
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/j5ftp2xrd2aps3c4mtn66umn.1493736693783%40email.android.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e12184ad-37ca-446b-83aa-25f4d4ae94a7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download

2017-05-02 Thread Antonis Christofides
> You cannot get a file outside of your project unless you symlink it inside the
> project. This is also a Very Bad Thing(TM) as it may allow attackers to
> request arbitrary files.
You /can/ get a file outside your project. Whether this is a bad thing or not
depends on why and how. I also don't think this file should be placed in the
static directory. The static directory is for files that don't change after the
program is installed. The media directory might be more appropriate.

If what you are doing is to dynamically create a csv file for your user to
download immediately, one possible way of doing that is (untested):

from tempfile import TemporaryFile

csvfile = TemporaryFile()
export_your_data_to_the_file(csvfile)
file_size = csvfile.tell()
csvfile.seek(0)
response = HttpResponse(csvfile.read(), content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=whateveryoulike.csv'
response['Content-Length'] = str(size)
return response

Regards,

A.

Antonis Christofides
http://djangodeployment.com

On 2017-05-02 17:53, m712 - Developer wrote:
>
> You cannot get a file outside of your project unless you symlink it inside the
> project. This is also a Very Bad Thing(TM) as it may allow attackers to
> request arbitrary files.
> What you should do instead is this:
> 1) Put Data/01/export.txt to the static/ folder inside your app (with the same
> folder structure if you need it that way)
> 2) Build the path to your file with /static/ as prefix, i.e.
> http://127.0.0.1/static/Data/01/export.txt
>
> On May 2, 2017 3:44 PM, Sixtine Vernhes  wrote:
>
> This is in development. I try to send url of my file in views.py :
>   
> |
>  returnrender(request,"export.html",{'out':urlOut})
> |
>
> and in my template I have the link :
>
> |
> Liendu fichier 
> |
>
> but when I open it I have this link :
>
> http://127.0.0.1:8000/home/myuser/Data/01/export.txt
>
> and I want to have this to download the file :
> /home/myuser/Data/01/export.txt
>
> Have you any ideas?
>
> (sorry i'm newbie in django ^^)
>
> Le mardi 2 mai 2017 14:04:05 UTC+2, Antonis Christofides a écrit :
>
> Is this in production or development? What is the url that doesn't
> work? What happens when you try the url?
>
> Regards,
>
> A.
>
> Antonis Christofides
> http://djangodeployment.com
>
> On 2017-05-02 11:28, Sixtine Vernhes wrote:
>> Hi !
>>
>> I try to create a link on Django who download a static csv file, but
>> I have no idea how to do.
>> In my settings.py :
>>
>> STATIC_URL = '/static/'
>> STATICFILES_DIRS = (
>> os.path.join(BASE_DIR, "static/"),
>> )
>>
>> and my file is in this directory
>>
>> Would anyone have an idea ?
>>
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to django-users...@googlegroups.com .
> -- 
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscr...@googlegroups.com
> .
> To post to this group, send email to django-users@googlegroups.com
> .
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> 
> https://groups.google.com/d/msgid/django-users/j5ftp2xrd2aps3c4mtn66umn.1493736693783%40email.android.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d73a67a4-49d9-ec8c-14f4-e3ccf44167d0%40djangodeployment.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download

2017-05-02 Thread m712 - Developer
You cannot get a file outside of your project unless you symlink it inside the 
project. This is also a Very Bad Thing(TM) as it may allow attackers to request 
arbitrary files.
What you should do instead is this:
1) Put Data/01/export.txt to the static/ folder inside your app (with the same 
folder structure if you need it that way)
2) Build the path to your file with /static/ as prefix, i.e. 
http://127.0.0.1/static/Data/01/export.txt

On May 2, 2017 3:44 PM, Sixtine Vernhes  wrote:
>
> This is in development. I try to send url of my file in views.py : 
>    
>  return render(request, "export.html", {'out': urlOut})
>
> and in my template I have the link : 
>
> Lien du  fichier 
>
> but when I open it I have this link : 
>
> http://127.0.0.1:8000/home/myuser/Data/01/export.txt 
>
> and I want to have this to download the file : 
> /home/myuser/Data/01/export.txt 
>
> Have you any ideas? 
>
> (sorry i'm newbie in django ^^)
>
> Le mardi 2 mai 2017 14:04:05 UTC+2, Antonis Christofides a écrit :
>>
>> Is this in production or development? What is the url that doesn't work? 
>> What happens when you try the url?
>>
>> Regards,
>>
>> A.
>>
>> Antonis Christofides
>>
>> http://djangodeployment.com
>>
>> On 2017-05-02 11:28, Sixtine Vernhes wrote:
>>>
>>> Hi ! 
>>>
>>> I try to create a link on Django who download a static csv file, but I have 
>>> no idea how to do. 
>>> In my settings.py :
>>>
>>> STATIC_URL = '/static/'
>>> STATICFILES_DIRS = (
>>>     os.path.join(BASE_DIR, "static/"),
>>> )
>>>
>>> and my file is in this directory 
>>>
>>> Would anyone have an idea ? 
>>>
>>>
>>>
>>> -- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to django-users...@googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/j5ftp2xrd2aps3c4mtn66umn.1493736693783%40email.android.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download

2017-05-02 Thread Sixtine Vernhes
This is in development. I try to send url of my file in views.py : 
   
 return render(request, "export.html", {'out': urlOut})

and in my template I have the link : 

Lien du  fichier 

but when I open it I have this link : 

http://127.0.0.1:8000/home/myuser/Data/01/export.txt 

and I want to have this to download the file : 
/home/myuser/Data/01/export.txt 

Have you any ideas? 

(sorry i'm newbie in django ^^)

Le mardi 2 mai 2017 14:04:05 UTC+2, Antonis Christofides a écrit :
>
> Is this in production or development? What is the url that doesn't work? 
> What happens when you try the url?
>
> Regards,
>
> A.
>
> Antonis Christofideshttp://djangodeployment.com
>
> On 2017-05-02 11:28, Sixtine Vernhes wrote:
>
> Hi ! 
>
> I try to create a link on Django who download a static csv file, but I 
> have no idea how to do. 
> In my settings.py :
>
> STATIC_URL = '/static/'
> STATICFILES_DIRS = (
> os.path.join(BASE_DIR, "static/"),
> )
>
> and my file is in this directory 
>
> Would anyone have an idea ? 
>
>  -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users...@googlegroups.com .
> To post to this group, send email to django...@googlegroups.com 
> .
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/c505a804-3cc6-4584-a47a-bffadee6fb5e%40googlegroups.com
>  
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e136ff80-bd25-4a06-ae08-9401e28c3c23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Link to download

2017-05-02 Thread Antonis Christofides
Is this in production or development? What is the url that doesn't work? What
happens when you try the url?

Regards,

A.

Antonis Christofides
http://djangodeployment.com

On 2017-05-02 11:28, Sixtine Vernhes wrote:
> Hi !
>
> I try to create a link on Django who download a static csv file, but I have no
> idea how to do.
> In my settings.py :
>
> STATIC_URL = '/static/'
> STATICFILES_DIRS = (
> os.path.join(BASE_DIR, "static/"),
> )
>
> and my file is in this directory
>
> Would anyone have an idea ?
>
> -- 
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com
> .
> To post to this group, send email to django-users@googlegroups.com
> .
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c505a804-3cc6-4584-a47a-bffadee6fb5e%40googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/59ec32d5-ba35-7421-0cca-bfc327ecc63e%40djangodeployment.com.
For more options, visit https://groups.google.com/d/optout.


Link to download

2017-05-02 Thread Sixtine Vernhes
Hi ! 

I try to create a link on Django who download a static csv file, but I have 
no idea how to do. 
In my settings.py :

STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static/"),
)

and my file is in this directory 

Would anyone have an idea ? 


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c505a804-3cc6-4584-a47a-bffadee6fb5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.