如何根据附件的标题自动填写表单字段?

时间:2018-08-10 08:06:27

标签: python html django forms

这是我的表格:

enter image description here

models.py:

class Document(models.Model):
    Document_name = models.CharField(max_length=255, default='Document_Name')
    Date = models.DateField()
    Client = models.ForeignKey(ClientDetail, on_delete=models.CASCADE)
    File = models.FileField()
    Filename = models.CharField(max_length=255)

    def __str__(self):
        return self.Document_name

upload.html:

<form method="post" enctype="multipart/form-data" style="margin-left: 16px">
    {% csrf_token %}
    <table>
        {{ form.as_table }}
    </table>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

forms.py:

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document
        fields = '__all__'

我想发生的事情是当我选择要附加的文档时,例如test1.pptx,我希望“文件名”字段使用该名称(test1.pptx)自动填充

1 个答案:

答案 0 :(得分:0)

将ID添加到字段

>>>p_txt =selector.xpath('//p[@class="title"]/descendant::text()').extract()
['#Who#', "I don't ", 'know', 'who you are']

然后在模板中

class Document(models.Model):
    Document_name = models.CharField(max_length=255, default='Document_Name',attrs={'id': 'file-name'})
    Date = models.DateField()
    Client = models.ForeignKey(ClientDetail, on_delete=models.CASCADE)
    File = models.FileField(attrs={'id': 'file-input'})
    Filename = models.CharField(max_length=255)

    def __str__(self):
        return self.Document_name

这将仅解析名称即可工作,一切都将像超级按钮一样工作