Lets suppose you need the user to select which files to download:
------choro_checkbox.html
<!DOCTYPE html>
<html>
<head>
<title>Choro's quertion about checkboxes</title>
</head>
<body>
<h1>Select files to download</h1>
<form method="POST">
{% csrf_token %}
<table>
<tr><th>File A</th><td><input type="checkbox" name="selected_file" 
value="a"></td></tr>
<tr><th>File B</th><td><input type="checkbox" name="selected_file" 
value="b"></td></tr>
<tr><th>File C</th><td><input type="checkbox" name="selected_file" 
value="c"></td></tr>
<tr><th>File D</th><td><input type="checkbox" name="selected_file" 
value="d"></td></tr>
<tr><th>File E</th><td><input type="checkbox" name="selected_file" 
value="e"></td></tr>
<tr><th>File F</th><td><input type="checkbox" name="selected_file" 
value="f"></td></tr>
<tr><th></th><td><input type="submit"></td></tr>
</table>
</form>
</body>
</html>

Now, you must watch which of the files the user selected, but you have a 
problem: if the user selected more than one file to download you cannot 
send the files directly to the user in the view as a response, instead, you 
must present them with a page to download the files:

def choro_checkbox_question(request):
    if request.method == 'GET':
        return render_to_response('testapp/choro_checkbox.html', 
context_instance=RequestContext(request))
    elif request.method == 'POST':
        selected_checkboxes = request.POST.getlist('selected_file', [])
        generated_filenames = []
        for selected_file in selected_checkboxes:
            filename = generate_file_for(selected_file)
            generated_filenames.append(filename)
        return render_to_response('testapp/choro_files_download.html', 
{'checks': selected_checkboxes,
                                                                        
'files': generated_filenames})

def generate_file_for(name):
    filename = os.path.join(settings.MEDIA_ROOT, name)
    filename = '{}.csv'.format(filename)
    with open(filename, mode='w') as f:
        f.write(name)
    return os.path.split(filename)[1]

-- choro_files_download.html
{% load static %}
{% get_media_prefix as MEDIA_URL %}
<!DOCTYPE html>
<html>
<head>
<title>Choro's quertion about checkboxes</title>
</head>
<body>
<h1>Select files to download</h1>
<p>{{ checks }}</p>
<p>
<ul>
{% for file in files %}
<li><a href="{{ MEDIA_URL }}{{ file }}">{{ file }}</a>
{% endfor %}
</ul>
</p>
</body>
</html>

You must correctly configure MEDIA_ROOT and MEDIA_URL in your settings, and 
may be use a better sub-directory schema to generate the files, but you can 
get the general idea.

Anyway, you still can verify if the user selected only one file and 
download that only file directly instead of presenting a page to download; 
you only present the page when the user selects more than one file to 
download.

Regards,
Camilo

On Monday, March 31, 2014 11:52:31 PM UTC-4:30, Choro H wrote:
>
> Thank you very much!
> I'm Sorry, i'm missing write comments,
> I want to do download have been checboxd file  from html, then i write to 
> views.py this code, so i don't know how do write !
> Please help me!
> this is my right code:
> def export_selected_data(request):
> #    if request.method == 'POST':
> #        _selected_action = request.POST.getlist("_
> selected_action")
>
>     response = HttpResponse(mimetype='application/vnd.ms-excel; 
> charset="Shift_JIS"')
>     response['Content-Disposition'] = 'attachment; filename=file.csv'
>     writer = csv.writer(response)
>     if request.method == 'POST':
>         _selected_action = request.POST.getlist("_selected_action")
>
>         _selected_action = User.objects.all()
>         for obj in _selected_action:
>             row=[]
>             for field in User._meta.fields:
>                 row.append(unicode(getattr(obj,field.name
> )).encode("cp932"))
>             writer.writerow(row)
>     return response
> This is my html:
>
> <form name="myForm"  method="POST">
> <select name="myMenu" onchange="myGo()">
> <option value="/articles/export_selected_data" >selected_export
> </select>
> </form>
> <table >
>         <thead>
>             <tr>
>                 <th>Check</th>              
>                 <th>名前</th>
>                 <th>会社名</th>
>                 <th>法人電話</th>
>             </tr>
>         </thead>
>         <tbody>
> {% if articles.count > 0 %}
> {% for user in articles %}
>         <tr>
>         <td ><input class="action-select" name="_selected_action" id 
> ="_selected_action" type="checkbox" value="{{user.id }}" type="submit" 
> class="button"></td>
>         <td ><a href="/articles/get/{{ user.id }}/">{{ user.user_name  
> }}</a></td>
>         <td >{{ user.company }}</td>
>         <td >{{ user.number }}</td> 
>         </tr>
> {% endfor %}
> </tbody>
> </table>
> {% else %}
>     <p>None</p>
> {% endif %}
>
> 2014年4月1日火曜日 12時47分08秒 UTC+9 Camilo Torres:
>>
>> Hello,
>>
>> You should start with the basics:
>> https://docs.djangoproject.com/en/1.6/topics/http/file-uploads/
>>
>> You are using request.FILES and that is for file upload, no file 
>> download. To upload a file to the server, you must have a <input 
>> type="file" ...> in your template, which you don't have.
>>
>> To download files from the server to the browser (to the user computer), 
>> you should do:
>> https://docs.djangoproject.com/en/1.6/ref/contrib/staticfiles/
>> or, if the user uploaded the content:
>> https://docs.djangoproject.com/en/1.6/topics/files/
>>
>> Hope this put you in the right direction.
>>
>> Camilo
>>
>> On Monday, March 31, 2014 1:31:24 AM UTC-4:30, Choro H wrote:
>>>
>>> Hello,
>>>
>>> i wan to do download file of a checkbox ;
>>> Please help me
>>>
>>> this is my viewspy:
>>>
>>> def export_selected_dataqq(request):
>>>     if request.method == 'POST':
>>>         _selected_action = request.FILES("_selected_action")
>>>        
>>>     response = HttpResponse(mimetype='application/vnd.ms-excel; 
>>> charset="Shift_JIS"')
>>>     response['Content-Disposition'] = 'attachment; filename=file.csv'
>>>     writer = csv.writer(response)
>>>     _selected_action = []
>>>     writer.writerow(_selected_action)
>>>    
>>>     for obj in _selected_action:
>>>         
>>>         row=[]
>>>         for field in User._meta.fields:
>>>             row.append(unicode(getattr(obj,field.name)).encode("cp932"))
>>>         writer.writerow(row)
>>>     return response
>>>
>>> this is mmy html:
>>>
>>> <table >
>>>         <thead>
>>>             <tr>
>>>                 <th>Check</th>              
>>>                 <th>名前</th>
>>>                 <th>会社名</th>
>>>                 <th>法人電話</th>
>>>             </tr>
>>>         </thead>
>>>         <tbody>
>>> {% if articles.count > 0 %}
>>> {% for user in articles %}
>>>         <tr>
>>>         <td ><input class="action-select" name="_selected_action" id 
>>> ="_selected_action" type="checkbox" value="{{user.id }}" type="submit" 
>>> class="button"></td>
>>>         <td ><a href="/articles/get/{{ user.id }}/">{{ user.user_name  
>>> }}</a></td>
>>>         <td >{{ user.company }}</td>
>>>         <td >{{ user.number }}</td> 
>>>         </tr>
>>> {% endfor %}
>>>
>>> </tbody>
>>> </table>
>>> </form>
>>> {% else %}
>>>     <p>None</p>
>>> {% endif %}
>>>
>>
>
> 2014年4月1日火曜日 12時47分08秒 UTC+9 Camilo Torres:
>>
>> Hello,
>>
>> You should start with the basics:
>> https://docs.djangoproject.com/en/1.6/topics/http/file-uploads/
>>
>> You are using request.FILES and that is for file upload, no file 
>> download. To upload a file to the server, you must have a <input 
>> type="file" ...> in your template, which you don't have.
>>
>> To download files from the server to the browser (to the user computer), 
>> you should do:
>> https://docs.djangoproject.com/en/1.6/ref/contrib/staticfiles/
>> or, if the user uploaded the content:
>> https://docs.djangoproject.com/en/1.6/topics/files/
>>
>> Hope this put you in the right direction.
>>
>> Camilo
>>
>> On Monday, March 31, 2014 1:31:24 AM UTC-4:30, Choro H wrote:
>>>
>>> Hello,
>>>
>>> i wan to do download file of a checkbox ;
>>> Please help me
>>>
>>> this is my viewspy:
>>>
>>> def export_selected_dataqq(request):
>>>     if request.method == 'POST':
>>>         _selected_action = request.FILES("_selected_action")
>>>        
>>>     response = HttpResponse(mimetype='application/vnd.ms-excel; 
>>> charset="Shift_JIS"')
>>>     response['Content-Disposition'] = 'attachment; filename=file.csv'
>>>     writer = csv.writer(response)
>>>     _selected_action = []
>>>     writer.writerow(_selected_action)
>>>    
>>>     for obj in _selected_action:
>>>         
>>>         row=[]
>>>         for field in User._meta.fields:
>>>             row.append(unicode(getattr(obj,field.name)).encode("cp932"))
>>>         writer.writerow(row)
>>>     return response
>>>
>>> this is mmy html:
>>>
>>> <table >
>>>         <thead>
>>>             <tr>
>>>                 <th>Check</th>              
>>>                 <th>名前</th>
>>>                 <th>会社名</th>
>>>                 <th>法人電話</th>
>>>             </tr>
>>>         </thead>
>>>         <tbody>
>>> {% if articles.count > 0 %}
>>> {% for user in articles %}
>>>         <tr>
>>>         <td ><input class="action-select" name="_selected_action" id 
>>> ="_selected_action" type="checkbox" value="{{user.id }}" type="submit" 
>>> class="button"></td>
>>>         <td ><a href="/articles/get/{{ user.id }}/">{{ user.user_name  
>>> }}</a></td>
>>>         <td >{{ user.company }}</td>
>>>         <td >{{ user.number }}</td> 
>>>         </tr>
>>> {% endfor %}
>>>
>>> </tbody>
>>> </table>
>>> </form>
>>> {% else %}
>>>     <p>None</p>
>>> {% endif %}
>>>
>>>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/248e0f60-c93b-448b-a691-670108703c63%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to