关于Django导入csv文件

时间:2014-03-06 02:20:21

标签: django

我无法从本地pc将csvfile导入数据库 请帮我如何导入csv

这是我的views.py:

def import_csv(请求):

upload_filename = 'csv_file'
if request.method == 'POST' and request.FILES.has_key('upload_filename'):       
    csvrawfile = request.FILES[upload_filename]

    reader = csv.reader(upload_filename)
    for row in reader:        
        t = User.objects.create()
        t.UserID = row[0]
        t.user_name = row[1]
        t.save()
    upload_filename.close()
    return  HttpResponseRedirect('/index/all')

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

这是我的importcsv.html:

{%extends“base.html”%} {%block content%}

                 {%csrf_token%}                            

{%endblock%}

0 个答案:

没有答案