Django FileField upload is not working for me

孤者浪人 提交于 2019-12-29 05:58:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!