Django FileField上传对我来说无效 [英] Django FileField upload is not working for me

查看:79
本文介绍了Django FileField上传对我来说无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在抓我的头FileField。 FileField需要一个单独的进程吗?



尽管我的url被保存..但是我的文件没有被上传...我在做什么错? >

这是我的models.py ...

 类OpLink(型号。模型):
user = models.ForeignKey(User)
file = models.FileField(blank = True,null = True,upload_to =uploads)
url = models.URLField = True,null = True)

我的forms.py



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

我的views.py

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

和我的html来处理它。

 < div class =span5> 
{{oplinkform.url | add_class:span4| attr:占位符:项目的URL}}
< br>< h4> OR< / h4>< br>
{{oplinkform.file | add_class:input-file}}
< br />
< input class ='btn btn-primary btn-large'type =submitvalue ='Post'name ='action'>
< / div>


解决方案

您需要在创建表单

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

还要确保您的表单具有正确的enctype

  form enctype =multipart / form-data>< / form> 


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>

解决方案

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>

这篇关于Django FileField上传对我来说无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆