问题样式图像上传按钮

2024-10-02 00:34:10 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我试图样式我的图片上传表单字段。根据这里另一个问题的指导,我最终做了以下工作:

document.getElementById("uploadBtn").onchange = function () { document.getElementById("uploadFile").value = this.value; };
.fileUpload {
    position: relative;
    overflow: hidden;
    margin: 10px;
  }
  .fileUpload input.upload {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    padding: 0;
    font-size: 20px;
    cursor: pointer;
    opacity: 0;
    filter: alpha(opacity=0);
  }
<input id="uploadFile" placeholder="Choose File" disabled="disabled"/>
                    <div class="fileUpload btn btn-primary">
                        <span>Upload</span>
                        <input id="uploadBtn" type="file" class="upload"/>
                    </div>

这是它应该工作的,但是如何将它绑定到django表单字段?如果我将输入中的class属性从“upload”替换为“profile\u pic”,就会出现主上传按钮,这是不好的。还是我做错了?我只需要把信息传给我的表格。你知道吗

photo = forms.ImageField(required=True, widget=forms.FileInput(attrs={'class': 'profile_pic'}))

Tags: marginidinputvaluepositiondocument单字class

热门问题