这是我第一次尝试实现'文件上传功能',我需要你的帮助。我正在研究遗留数据库,我应该在以这种方式创建的表中进行文件上传:
CREATE TABLE "LICENCE"
( "ID" NUMBER NOT NULL ENABLE,
"VEH_ID" NUMBER NOT NULL ENABLE,
"DTP_ID" NUMBER NOT NULL ENABLE,
"LICENCENO" VARCHAR2(50 CHAR) NOT NULL ENABLE,
"ISSUEDATE" DATE,
"STARTDATE" DATE,
"EXPIREDATE" DATE,
"DOCPATH" VARCHAR2(500 CHAR),
"CHECKFLAG" NUMBER(1,0) NOT NULL ENABLE,
CONSTRAINT "LIC_PK" PRIMARY KEY ("ID") ENABLE,
CONSTRAINT "LIC_DTP_FK" FOREIGN KEY ("DTP_ID")
REFERENCES "DOCTYPES" ("ID") ENABLE,
CONSTRAINT "LIC_VEH_FK" FOREIGN KEY ("VEH_ID")
REFERENCES "VEHICLES" ("ID") ENABLE)/
使用inspectdb,我得到了这个模型:
class Licence(models.Model):
id = models.DecimalField(unique=True, primary_key=True, max_digits=127, decimal_places=0)
veh_id = models.ForeignKey(Vehicles, db_column='veh_id')
dtp_id = models.ForeignKey(Doctypes, db_column='dtp_id')
licenceno = models.CharField(max_length=200)
issuedate = models.DateField(null=True, blank=True)
startdate = models.DateField(null=True, blank=True)
expiredate = models.DateField(max_length=2000, blank=True)
docpath = models.CharField(max_length=200)
checkflag = models.IntegerField()
def __unicode__(self):
return self.licenceno
我应该如何处理上传?阅读django文件上传文档后我的想法是将docpath修改为filefield。这是正确的做法吗?因为我尝试了它并且表单没有通过验证,而我得到“没有选择文件”。有人可以指导我吗?
我的views.py是:
def upload(request):
if "doc-form" in request.POST:
form = LicenceForm(data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
return render_to_response('bingo.html', locals(), context_instance=RequestContext(request)
else:
form = LicenceForm()
return render_to_response('upload.html', locals(), context_instance=RequestContext(request))
以及我模板中的表单:
<form enctype="multipart/form-data" id="insertDocsForm" method="post" action='/upload/'>{% csrf_token %}
<div class="id">
{{ form.id.errors }}
<label for="id"></label>
{{ form.id }}
</div>
<div class="veh_id">
{{ form.veh_id.errors }}
<label for="veh_id">veh:</label>
{{ form.veh_id }}
</div>
<div class="dtp_id">
{{ form.dtp_id.errors }}
<label for="dtp_id">dpt:</label>
{{ form.dtp_id }}
</div>
<div class="licenceno">
{{ form.licenceno.errors }}
<label for="licenceno">lic:</label>
{{ form.licenceno }}
</div>
<div class="issuedate">
{{ form.issuedate.errors }}
<label for="issuedate">isdate:</label>
{{ form.issuedate }}
</div>
<div class="startdate">
{{ form.startdate.errors }}
<label for="startdate">stdate</label>
{{ form.startdate }}
</div>
<div class="expiredate" >
{{ docform.expiredate.errors }}
<label for="expiredate">exdate:</label>
{{ form.expiredate }}
</div>
<div class="docpath">
{{ form.docpath.errors }}
<label for="docpath"></label>
{{ form.docpath }}
</div>
<div class="checkflag">
{{ form.checkflag.errors }}
<label for="checkflag"></label>
{{ form.checkflag }}
</div>
<p>{{ form.errors }}
{{ form.docpath.errors }}
</p>
<input type="hidden" name="doc-type" value="doc-form"
<input type="submit" id="id_docSubmit" value="Upload">
</form>
答案 0 :(得分:0)
您需要在模型中包含FileField
字段,该字段将存储并充当文件。你的模型似乎没有。
HTML表单另一个警告是enctype="multipart/form-data"
喜欢:
<form enctype="multipart/form-data" method="post" action="/foo/">