Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django FileField upload is not working for me

I have been scratching my head FileField. Does the FileField require a seperate process?

Although my url gets saved .. but my file doesn't get uploaded ... what am i doing wrong?

This is my models.py ...

class OpLink(models.Model):
    user = models.ForeignKey(User)
    file = models.FileField(blank=True, null=True, upload_to="uploads")
    url = models.URLField(blank=True, null=True)

my forms.py

class OpLinkForm(ModelForm):
    class Meta:
        model = OpLink
        exclude = ('user')

my views.py

oplinkform = oplinkform(request.POST)
                oplink = oplinkform.save(commit=False)
                oplink.user = user
                oplink.save()

and my html to process it.

<div class="span5">
                            {{ oplinkform.url|add_class:"span4"|attr:"Placeholder:URL for the item" }}
                            <br><h4>OR</h4><br>
                            {{ oplinkform.file|add_class:"input-file" }}
                            <br />
                            <input class='btn btn-primary btn-large' type="submit" value='Post' name='action'>
</div>
like image 465
Yousuf Jawwad Avatar asked Aug 16 '12 09:08

Yousuf Jawwad


People also ask

How to upload documents in Django?

Django provides built-in library and methods that help to upload a file to the server. The forms. FileField() method is used to create a file input and submit the file to the server. While working with files, make sure the HTML form tag contains enctype="multipart/form-data" property.


1 Answers

You need to include the files when creating the form

oplinkform = oplinkform(request.POST, request.FILES)

Also make sure that your form has the correct enctype

<form enctype="multipart/form-data"></form>
like image 170
Mikael Avatar answered Oct 05 '22 18:10

Mikael