2

It's my first time trying to achieve the 'file upload functionality' and I need your help. I'm working on a legacy db and I'm supposed to do the file upload in a table that gets created this way:

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)/

With inspectdb, I got this model:

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

How should I handle the upload?My thought after reading the django file upload documentation was to modify the docpath to filefield. Is that the right practice?Because I tried it and the form doesn't pass validation while I get "No file chosen". Can someone guide me through?

my views.py is:

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))

and the form in my template:

<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>
rous
  • 21
  • 2

1 Answers1

0

You need to have FileField field in the model that will store and act as file. Your model doesn't seem to have that.

There is another caution with HTML form is that enctype="multipart/form-data" like :

<form enctype="multipart/form-data" method="post" action="/foo/">
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • I tried both of what you suggest(I mention it) but I get the error that I also mention in my question. what can go wrong? i followed this post: http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example/8542030#8542030 – rous Sep 09 '12 at 16:55
  • @rous Can you show your form, template? Do you get any files in `request.FILES`? – Rohan Sep 10 '12 at 03:48
  • I added the form in the template. I also tried `print request.FILES` and I get ! Any ideas? – rous Sep 10 '12 at 12:26
  • It's a modelform. I just do the classics: `class LicenceForm(ModelForm): class Meta: model = Licence` – rous Sep 10 '12 at 13:04
  • I also try `print request.raw_post_data` and the name of the file appears in the post data. – rous Sep 10 '12 at 13:26
  • `docpath` is not a file field so I'm not sure how you are able to choose file for this field. May be some information is missing? – Rohan Sep 11 '12 at 03:57