Django FileField 上传对我不起作用 [英] Django FileField upload is not working for me
本文介绍了Django FileField 上传对我不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在挠头 FileField.FileField 是否需要单独的过程?
虽然我的网址被保存了..但我的文件没有上传......我做错了什么?
这是我的models.py ...
class OpLink(models.Model):用户 = 模型.外键(用户)file = models.FileField(blank=True, null=True, upload_to="uploads")url = 模型.URLField(空白=真,空=真)
我的表单.py
class OpLinkForm(ModelForm):元类:模型 = OpLink排除 = ('用户')
我的views.py
oplinkform = oplinkform(request.POST)oplink = oplinkform.save(commit=False)oplink.user = 用户oplink.save()
和我的 html 来处理它.
{{ oplinkform.url|add_class:"span4"|attr:"Placeholder:项目的URL" }}<br><h4>OR</h4><br>{{ oplinkform.file|add_class:"输入文件" }}<br/><input class='btn btn-primary btn-large' type="submit" value='Post' name='action'>
解决方案
创建表单时需要包含文件
oplinkform = oplinkform(request.POST, request.FILES)
还要确保您的表单具有正确的加密类型
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屋!
查看全文