Python Django |由于对象没有属性',使用表单上载图像不工作;清理数据';
我想在数据库中上载一个in图像,但在阅读了大量教程后,由于打印表单后出现此错误,此代码无法工作。错误:Python Django |由于对象没有属性',使用表单上载图像不工作;清理数据';,python,django,image-uploading,Python,Django,Image Uploading,我想在数据库中上载一个in图像,但在阅读了大量教程后,由于打印表单后出现此错误,此代码无法工作。错误: **AttributeError at /upload** 'ImageUploadForm' object has no attribute 'cleaned_data' 以下是我的代码: 型号。py: class room_type(models.Model): id = models.IntegerField(primary_key = True) code = mod
**AttributeError at /upload**
'ImageUploadForm' object has no attribute 'cleaned_data'
以下是我的代码:型号。py:
class room_type(models.Model):
id = models.IntegerField(primary_key = True)
code = models.CharField(max_length = 40)
name = models.CharField(max_length= 40 )
title = models.CharField(max_length = 40)
english_title = models.CharField(max_length=40)
capacity = models.IntegerField()
extra_capacity = models.IntegerField()
description = models.CharField(max_length=255)
today_price = models.IntegerField()
class Meta:
db_table = 'room_types'
def __unicode__(self):
return u'%d' % (self.id)
class attachment(models.Model):
id = models.IntegerField(primary_key = True)
image = models.ImageField(upload_to="/home/iman/Desktop/code/hotel/rooms/attachments")
#foreign key : a room has many images
rt_id = models.ForeignKey(room_type)
class Meta:
db_table = 'attachments'
def __unicode__(self):
return u'%d' % (self.id)
class ImageUploadForm(forms.Form):
image = forms.ImageField()
class Meta:
model = attachment
fields= ['image']
forms.py:
class room_type(models.Model):
id = models.IntegerField(primary_key = True)
code = models.CharField(max_length = 40)
name = models.CharField(max_length= 40 )
title = models.CharField(max_length = 40)
english_title = models.CharField(max_length=40)
capacity = models.IntegerField()
extra_capacity = models.IntegerField()
description = models.CharField(max_length=255)
today_price = models.IntegerField()
class Meta:
db_table = 'room_types'
def __unicode__(self):
return u'%d' % (self.id)
class attachment(models.Model):
id = models.IntegerField(primary_key = True)
image = models.ImageField(upload_to="/home/iman/Desktop/code/hotel/rooms/attachments")
#foreign key : a room has many images
rt_id = models.ForeignKey(room_type)
class Meta:
db_table = 'attachments'
def __unicode__(self):
return u'%d' % (self.id)
class ImageUploadForm(forms.Form):
image = forms.ImageField()
class Meta:
model = attachment
fields= ['image']
视图。py:
def upload(request):
if request.method == 'POST':
print"##########"
form = ImageUploadForm(request.POST,request.FILES)
if form.is_valid():
new_attachment = attachment()
new_attachment.image= form.cleaned_data['image']
new_attachment.id = 1
new_attachment.rt_id = 1
new_attachment.save()
print "))))))))"
#return HttpResponse('image upload success')
else:
print form.is_valid()
print form.errors
form = ImageUploadForm()
return render(request,'index.html')
index.html:
<div id="dialog" title="آپلود فایل">
<form action="{% url 'upload' %}" class="dropzone" enctype="multipart/form-data" id="my-awesome-dropzone" method="post" >{% csrf_token %}
<input id="id_image" type="file" class="" name="image">
<input type="submit" value="Submit" />
</form>
</div>
> full traceback : Environment:
>
>
> Request Method: POST Request URL: http://localhost:8000/upload
>
> Django Version: 1.6.5 Python Version: 2.7.3 Installed Applications:
> ('django.contrib.admin', 'django.contrib.auth',
> 'django.contrib.contenttypes', 'django.contrib.sessions',
> 'django.contrib.messages', 'django.contrib.staticfiles', 'rooms')
> Installed Middleware:
> ('django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.common.CommonMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'django.middleware.clickjacking.XFrameOptionsMiddleware')
>
>
> Traceback: File
> "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py"
> in get_response
> 112. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/iman/Desktop/code/hotel/rooms/views.py" in upload
> 147. print form.cleaned_data['image']
>
> Exception Type: AttributeError at /upload Exception Value:
> 'ImageUploadForm' object has no attribute 'cleaned_data'
{%csrf_令牌%}
>完全回溯:环境:
>
>
>请求方法:发布请求URL:http://localhost:8000/upload
>
>Django版本:1.6.5 Python版本:2.7.3已安装的应用程序:
>('django.contrib.admin'、'django.contrib.auth',
>'django.contrib.contenttypes','django.contrib.sessions',
>'django.contrib.messages'、'django.contrib.staticfiles'、'rooms')
>已安装的中间件:
>('django.contrib.sessions.middleware.SessionMiddleware',
>'django.middleware.common.CommonMiddleware',
>“django.middleware.csrf.CsrfViewMiddleware”,
>'django.contrib.auth.middleware.AuthenticationMiddleware',
>'django.contrib.messages.middleware.MessageMiddleware',
>'django.middleware.clickjacking.xframeoptions中间件')
>
>
>回溯:文件
>“/usr/local/lib/python2.7/dist packages/django/core/handlers/base.py”
>在得到答复时
> 112. response=upload中的wrapped_callback(请求,*callback_args,**callback_kwargs)文件“/home/iman/Desktop/code/hotel/rooms/views.py”
> 147. 打印表单。已清理的_数据['image']
>
>异常类型:AttributeError at/上载异常值:
>“ImageUploadForm”对象没有“cleaned_data”属性
尝试删除
class Meta:
model = attachment
html中的表单缺少表单字段。不,请看这个链接plz:和这个链接:我认为dropzone可以与ajax一起工作。。因此,您需要编写用csrf令牌处理ajax调用的逻辑。我不确定,但我认为在代码中的某个地方,您调用了
表单。在调用表单之前清理了\u data
。\u valid()
(在读取回溯后)表单中是否存在name=image的输入字段?