about csv import

24 views
Skip to first unread message

Choro Hasu

unread,
Mar 4, 2014, 6:15:49 AM3/4/14
to django...@googlegroups.com
hello:

I could not  import csv to SnetOS from local pc,  please tell me 

this is my views.py file :
def import_csv(request):
    upload_filename = 'csvfile'
    if request.method == 'POST' and request.FILES.has_key('upload_filename'):
        csv_file = request.FILES['csv_file']
        
        response = HttpResponse(mimetype='application/vnd.ms-excel; charset=UTF-8')
        response['Content-Disposition'] = 'attachment; filename=file.csv'
        reader = csv.reader(response)
       
        reader = csv.reader(csv_file)
        for row in reader:          
            tmp = User.objects.create()
            tmp.UserID = row[0]
            tmp.user_name = row[1]
            tmp.first_kana = row[2]
            tmp.last_kana = row[3]            
            
            tmp.save()
        csv_file.close()
        return  HttpResponseRedirect('/memo/all')
    else:

        tmp = User.objects.all()
    args = {}
    args.update(csrf(request))
    args['tmp'] = tmp
    return render_to_response('importcsv.html', args)   

this is my html:
{% extends "base.html" %}

{% block content %}
<div>
    <form action="" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            {{ form.as_table }}
                   <input type="file" name="csvfile">
        <input type="submit" value="import" id="Save">
    </form>
</div>

{% endblock %}


Reply all
Reply to author
Forward
0 new messages