问题
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>
回答1:
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>
来源:https://stackoverflow.com/questions/11983885/django-filefield-upload-is-not-working-for-me