csv_import()只取1个参数(给定0)

时间:2014-03-27 11:16:16

标签: python django

嘿我正在尝试使用DJANGO视图上传csv文件,但我收到此错误 csv_import()只取1个参数(给定0) 我的代码是 图

def csv_import(request, **kwargs):
    if request.method == "POST":
        form = DataInput(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            success = True
            context = {"form": form, "success": success}
            return render_to_response("imported.html", context,
                                      context_instance=RequestContext(request))
    else:
        form = DataInput()
        context = {"form": form}
        return render_to_response("imported.html", context,
                                  context_instance=RequestContext(request))

形式

class DataInput(forms.Form):
    file = forms.FileField()


def save(self):
    records = csv.reader(self.cleaned_data["file"])
    for line in records:
        parts = Part()
        parts.supplier_id = line[0]
        parts.name = line[1]
        parts.description = line[2]
        parts.save()

URL

url(r'^imported',views.csv_import(),name="imported")

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

尝试将views.csv_import ()作为第二个参数传递给url

您不想立即调用它,但在匹配请求进入时告诉url要调用哪个函数。