I couldn t find this in the docs, but think it must be possible. I m talking specifically of the ClearableFileInput widget. From a project in django 1.2.6 i have this form:
# the profile picture upload form
class ProfileImageUploadForm(forms.ModelForm):
"""
simple form for uploading an image. only a filefield is provided
"""
delete = forms.BooleanField(required=False,widget=forms.CheckboxInput())
def save(self):
# some stuff here to check if "delete" is checked
# and then delete the file
# 8 lines
def is_valid(self):
# some more stuff here to make the form valid
# allthough the file input field is empty
# another 8 lines
class Meta:
model = SocialUserProfile
fields = ( image ,)
i 然后使用该模板代码:
<form action="/profile/edit/" method="post" enctype="multipart/form-data">
Delete your image:
<label> {{ upload_form.delete }} Ok, delete </label>
<button name="delete_image" type="submit" value="Save">Delete Image</button>
Or upload a new image:
{{ upload_form.image }}
<button name="upload_image" type="submit" value="Save">Start Upload</button>
{% csrf_token %}
</form>
由于Django 1.3.1现在将可清除的液体投入用作缺损植被,即表面上可以绕过我形式的16条线。 除此以外,也只是缩短了类似形式代码:
# the profile picture upload form
class ProfileImageUploadForm(forms.ModelForm):
"""
simple form for uploading an image. only a filefield is provided
"""
class Meta:
model = SocialUserProfile
fields = ( image ,)
这将给我带来一种良好感觉,即,我不太定制的形式编码,而且可以依靠Django的建筑。
I would, of course, like to keep the html-output the same as before. When just use the existing template code, such things like "Currently: somefilename.png" pop up at places where i do not want them.
象<条码>{上载_form.image.file>/code”等进一步铺设表格似乎不可行。 我所想的下一个事情是写出一种习俗。 这将完全违背我为尽可能取消尽可能多的定制守则所作的努力。
在这种情况下,什么是最简单的事?