[英]Saving file in Django doesn't work
I've created a form
which should gets text,modelchoice and file
. 我创建了一个
form
,该form
应获取text,modelchoice and file
。 Everything works correctly instead of file
saving. 一切正常,而不是
file
保存。 It doesn't returns any error
but does not save any file. 它不返回任何
error
但不保存任何文件。
So this is my form: 这是我的表格:
class PurchaseForm(forms.Form):
text = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 18, 'cols': 90}))
language = forms.ModelChoiceField(queryset=Language.objects.all())
file = forms.FileField(required=False)
This is a model called job: 这是一个称为作业的模型:
class Job(models.Model):
customer = models.ForeignKey(User,null=False,related_name='customer')
freelancer = models.ForeignKey(User,null=True,related_name='freelancer')
description = models.TextField(null=True)
price = models.FloatField(null=True)
language = models.ForeignKey(Language,null=False)
file = models.FileField(upload_to='files')
And this is the view: 这是视图:
def purchase(request):
if request.method == 'POST':
print dict(request.POST.iterlists())
form = forms.PurchaseForm(request.POST)
if form.is_valid():
job = Job()
print form.cleaned_data
job.customer = request.user
job.language = form.cleaned_data['language']
# job.description = form.cleaned_data['description']
if 'file' in form.cleaned_data.keys():
print 'OKOK'
job.file = form.cleaned_data['file']
# print job.file.path # If I uncomment this, it raises error Exception Value: The 'file' attribute has no file associated with it.
job.save()
return render(request, 'purchases/complete.html')
else:
purchase_form = forms.PurchaseForm()
return render(request, 'jobs/purchase.html', {'purchase_form': purchase_form})
In this view
there are two prints
. 在此
view
,有两个prints
。 The first prints this (I can see the file there): 第一个打印此文件(我可以在此处看到文件):
{u'text': [u'\u010dr\u010dr\u010dr\u010d'], u'csrfmiddlewaretoken': [u'os8p9oRWR
d8mMTmSsGPd55Y7bWy6pMWl'], u'language': [u'3'], u'file': [u'export.csv']}
And the second prints this: 第二个显示:
{'text': u'\u010dr\u010dr\u010dr\u010d', 'file': None, 'language': <Language: HG
: Hungar - Intermediate - 0.02 eur/sign>}
It act's like I didn't upload (send) any file through the form
. 就像我没有通过
form
上传(发送)任何文件一样。
Do you have any idea why is that so? 您知道为什么会这样吗?
EDIT: If I leave the print job.file.path
line uncommented, it raises this error: Exception Value: 编辑:如果我不注释
print job.file.path
行,它将引发此错误:异常值:
The 'file' attribute has no file associated with it.
I think you need update form like this: 我认为您需要这样的更新表格:
def purchase(request):
if request.method == 'POST':
print dict(request.POST.iterlists())
form = forms.PurchaseForm(request.POST request.FILES or None)
UPD for comment: UPD发表评论:
for saving try too add this in url
: 为了保存,也尝试在
url
添加:
from django.conf.urls.static import static
urlpatterns = [...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and check media
settings: 并检查
media
设置:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this is for admin, add method for model, which use image field: 这是给管理员的,为模型添加方法,它使用图像字段:
def admin_image(self):
return "<img src='%'/>" % self.your_image_field.url
admin_image.allow_tags = True
and register list_display=('admin_image',)
in admin.py
并在
admin.py
注册list_display=('admin_image',)
admin.py
list_display=('admin_image',)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.