2012-09-09 73 views
2

這是我第一次嘗試實現'文件上傳功能',我需要你的幫助。我工作的一個傳統的數據庫,我應該做的文件上傳在被創建這樣一個表:modelform不驗證文件上傳

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

您必須將存儲,並作爲文件模型FileField領域。你的模型似乎沒有。

沒有與HTML表單的另一個注意事項是enctype="multipart/form-data",如:

<form enctype="multipart/form-data" method="post" action="/foo/"> 
+0

我想你們倆有什麼建議(我提到它),但我得到的是我還提及我的問題的錯誤。什麼可能出錯?我跟着這篇文章:http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example/8542030#8542030 – rous

+0

@rous你可以顯示你的表單,模板? 'request.FILES'中有沒有文件? – Rohan

+0

我在模板中添加了表單。我也嘗試過'print request.FILES',我得到!有任何想法嗎? – rous