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>
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With