Django python checkbox

249 views
Skip to first unread message

Choro H

unread,
Mar 31, 2014, 2:01:24 AM3/31/14
to django...@googlegroups.com
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 %}

Camilo Torres

unread,
Mar 31, 2014, 11:47:08 PM3/31/14
to django...@googlegroups.com
Hello,

You should start with the basics:

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:
or, if the user uploaded the content:

Hope this put you in the right direction.

Camilo
Message has been deleted

Choro H

unread,
Apr 1, 2014, 12:22:31 AM4/1/14
to django...@googlegroups.com
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:


2014年4月1日火曜日 12時47分08秒 UTC+9 Camilo Torres:

Choro H

unread,
Apr 1, 2014, 9:21:56 PM4/1/14
to django...@googlegroups.com
Sorry you all,

 so,i want to export csv from database that is only when checked  


2014年4月1日火曜日 13時22分31秒 UTC+9 Choro H:

Mario Gudelj

unread,
Apr 1, 2014, 10:22:55 PM4/1/14
to django...@googlegroups.com
There are a few ways in which you can do this. One of them is to add some javascript to your code that will listen to onclick and check if your input button is selected. If it is it will submit the form that initiates your download. You can also do an Ajax post. Another way would be to create a link to that view. I don't understand why it needs to be checkbox if it's not a part of the form the user is submitting. 


--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d15a22b7-bbd2-4193-9a1a-49139e4b0ee0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Camilo Torres

unread,
Apr 1, 2014, 10:41:11 PM4/1/14
to django...@googlegroups.com
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

Choro H

unread,
Apr 2, 2014, 5:53:53 AM4/2/14
to django...@googlegroups.com

Thank you very much.
For example, i want to export only checked of a lot of information, solike  the following image:



2014年4月2日水曜日 11時22分55秒 UTC+9 somecallitblues:
Message has been deleted

Choro H

unread,
Apr 2, 2014, 6:00:08 AM4/2/14
to django...@googlegroups.com



2014年4月2日水曜日 18時53分53秒 UTC+9 Choro H:

Choro H

unread,
Apr 2, 2014, 6:01:48 AM4/2/14
to django...@googlegroups.com
Thank you very much!  This was helpful

2014年4月2日水曜日 11時41分11秒 UTC+9 Camilo Torres:
Reply all
Reply to author
Forward
0 new messages