form doesn't validate when trying to upload file

35 views
Skip to first unread message

mapapage

unread,
Sep 9, 2012, 10:56:26 AM9/9/12
to django...@googlegroups.com
Hi! 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 table:
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 file upload documentation was to modify the docpath to filefield. Is that the right practice?Because I tried it, and the form doesn't validate, I get the error "No file chosen". Can anyone guide me through this?Thank you..

Joseph Mutumi

unread,
Sep 9, 2012, 2:29:11 PM9/9/12
to django...@googlegroups.com
I'm assuming you are using the forms.ModelForm for your model? Say LicenceForm?

class LicenseForm(forms.ModelForm):
    class Meta:
        model = License 

According to the doc you have to pass all relevant QueryDicts to the form __init__
when you are creating a bound instance. In a nutshell make sure your doing:

# The view function
def handle_upload(request):
    # Get request logic ...
    if request.method == "POST":
        form = LicenceForm(request.POST, request.FILES)
        if form.is_valid():
           #save input data
           form.save()
           #continue with other stuff

More on the docs: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#handling-uploaded-files-with-a-model


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/eo_e4m3hye8J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

mapapage

unread,
Sep 9, 2012, 2:40:37 PM9/9/12
to django...@googlegroups.com
Yes, exactly I'm using a modelform and in my views I'm doing:
def upload(request):
if "doc-form" in request.POST:
        docform = LicenceForm(data=request.POST, files=request.FILES)
if docform.is_valid():
            docform.save()
           
return render_to_response('bingo.html', locals(), context_instance=RequestContext(request)
   
else:
        docform
= LicenceForm()
return render_to_response('upload.html', locals(), context_instance=RequestContext(request))

but it doesn't work!
I also include the right header in the form template and changed the directories in settings. I really can't sense the problem..

Kurtis Mullins

unread,
Sep 9, 2012, 3:15:27 PM9/9/12
to django...@googlegroups.com
Also, don't forget the multi-part in your template: <FORM action="" enctype="multipart/form-data" method="post">

mapapage

unread,
Sep 10, 2012, 8:41:28 AM9/10/12
to django...@googlegroups.com
as I say in my last reply, I include the multi part header and it doesn't work! :( What else I can try?

Kurtis Mullins

unread,
Sep 10, 2012, 10:18:22 AM9/10/12
to django...@googlegroups.com
ahh, sorry I just saw something that might help. Try making this change:

docpath = models.CharField(max_length=200)

to:

docpath = models.FileField(...) # Check the docs for arguments to this Field. https://docs.djangoproject.com/en/dev/ref/models/fields/#filefield

On Mon, Sep 10, 2012 at 8:41 AM, mapapage <mapa...@gmail.com> wrote:
as I say in my last reply, I include the multi part header and it doesn't work! :( What else I can try?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/NpTYlQAR80sJ.

mapapage

unread,
Sep 10, 2012, 10:38:45 AM9/10/12
to django...@googlegroups.com
I have it like this, with filefield, in my first question I'm just posting the generated model from inspectdb and underneath I'm saying that I change it to FileField..

Kurtis Mullins

unread,
Sep 10, 2012, 10:45:23 AM9/10/12
to django...@googlegroups.com
Sorry about that. I accidentally over-looked where you mentioned that in your email.

Are you running this on a debug server? Also, can you try replacing your template code with a simple <form ....>{% csrf_token %}{{ form }}<input type="submit" /></form> and see if that makes any differences?

On Mon, Sep 10, 2012 at 10:38 AM, mapapage <mapa...@gmail.com> wrote:
I have it like this, with filefield, in my first question I'm just posting the generated model from inspectdb and underneath I'm saying that I change it to FileField..

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/LYyL0YvwrVUJ.

mapapage

unread,
Sep 10, 2012, 11:05:56 AM9/10/12
to django...@googlegroups.com
I'm running it on the django development server. I changed the header and it behaves  the same way. Is that expected or not?
I also tried print request.FILES and I get <MultiValueDict: {}>, but after i tried print request.raw_post_data and it seems that the name of the file that i try to upload is included in the post data. I'm running out of other ideas!

mapapage

unread,
Sep 10, 2012, 4:12:54 PM9/10/12
to django...@googlegroups.com
After some testing i think that the prob occurs because from the same view I render another one form in the same template. This form is not multi-part. In this case it happens what I describe. If i put this simple form in comments (in views and in template) the multipart form uploads the file normally. Isn't it possible to handle these 2 forms in the same view?
 This is what I do:
def upload(request):
    c = {}
    c.update(csrf(request))

    if "vehicle-form" in request.POST:
        form = VForm(data=request.POST, own_id=u_id)
        print form.is_valid()
        if form.is_valid():
            form.save()
            return render_to_response('bingo.html', locals(), context_instance= RequestContext(request))
    else:
        form = VForm()
        
    if "doc-form" in request.POST: 
        form = LicenceForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES["docpath"])
            form.save()
            return render_to_response('success.html', locals(), context_instance=RequestContext(request))
    else:
Reply all
Reply to author
Forward
0 new messages