我有一个像下面这样的模型
模型.py
class Publisher(models.Model):
name = models.CharField(max_length=256)
image = models.ImageField(upload_to='/media/')
为了设计目的,我必须添加一些 id 等属性
表格.py
class PublisherForm(ModelForm):
class Meta:
model = Publisher
def __init__(self, *args, **kwargs):
super(PublisherForm, self).__init__(*args, **kwargs)
self.fields['image'].widget.attrs = {'id':'selectedFile', 'style':'display: none;'}
模板.py
<html>
<body>
<form enctype="multipart/form-data" action="{% url 'add_a_book' %}" method="post">
{{book_form.name}}
{{book_form.image}}
<input type="button" value="Upload image" onclick="document.getElementById('selectedFile').click();" class="big_btn " />
input type="button" value="Clear" onclick="window.location.href='{% url 'clear_image_for_pubsliher' book_id %}'" class="big_btn " />
<input type="submit" value="submit">
</form>
</body>
</html>
例如,如果用户从上面的browse( Upload image
) 按钮中选择了一张图片,那么图片路径将类似于'image/akjsdhsa56453121/ping.png'
.
那么是否有可能通过编写单独的javascript函数来获取上述函数image path('image/akjsdhsa56453121/ping.png')
?* 表格提交前 *onclick javascript
Upload image
因为通过使用该值,我将立即显示用户选择的所选图像