Django编辑表单未显示附件链接
下午好 我在尝试创建编辑模板时遇到一个查询,我通过api调用检索数据。当我在django form.Forms中输入数据时,附件显示为空白 这是Form.py文件Django编辑表单未显示附件链接,django,django-forms,Django,Django Forms,下午好 我在尝试创建编辑模板时遇到一个查询,我通过api调用检索数据。当我在django form.Forms中输入数据时,附件显示为空白 这是Form.py文件 class PaymentListForm(forms.Form): Title = forms.CharField(max_length=50) Receipt_Amount = forms.DecimalField(max_digits=8, decimal_places=2) Request_Amount = fo
class PaymentListForm(forms.Form):
Title = forms.CharField(max_length=50)
Receipt_Amount = forms.DecimalField(max_digits=8, decimal_places=2)
Request_Amount = forms.DecimalField(max_digits=8, decimal_places=2)
Receipt_Attachment = forms.FileField()
def __init__(self, *args, **kwargs):
super(PaymentListForm, self).__init__(*args, **kwargs)
这是views.py文件
def edit_payments(request, trn_pk):
trns = get_transaction_by_id(trn_pk)
data = trns['data']
if request.method == 'GET':
form = PaymentListForm(initial=data)
return render(request, 'paymentlist/createeditpayments.html', {'form': form})
我可以确认,在数据字典中有附件的链接,但不知何故,在表格中附件丢失了
有人帮忙吗
感谢无法像在CharField或DecimalField中那样为FileField设置初始值,但有一种方法可以解决此问题。我们需要的是隐藏forms.FileField并通过标签处理加载文件,并将初始数据显示为文本。因此,首先隐藏forms.FileField: forms.py:
class PaymentListForm(forms.Form):
Title = forms.CharField(max_length=50)
Receipt_Amount = forms.DecimalField(max_digits=8, decimal_places=2)
Request_Amount = forms.DecimalField(max_digits=8, decimal_places=2)
Receipt_Attachment = forms.FileField(label="", widget=forms.FileInput(
attrs={'style': "display: none",},
), )
def __init__(self, *args, **kwargs):
super(PaymentListForm, self).__init__(*args, **kwargs)
<form method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<span>Receipt attachment: </span>
<label for="id_Receipt_Attachment" class="mt-2 btn btn-primary " style="cursor: pointer;">Choose file...</label>
<span id="receipt_url"> {{ url_of_receipt }}</span>
<br><br>
<button class="btn btn-primary" role="button" type="submit">
Save
</button>
def edit_payments(request, trn_pk):
trns = get_transaction_by_id(trn_pk)
data = trns['data']
if request.method == 'GET':
form = PaymentListForm(initial=data)
return render(request, 'paymentlist/createeditpayments.html', {'form': form, 'url_of_receipt': data['Receipt_Attachment']})
现在我们需要添加标签来处理上传文件,而不是forms.FileField和标签,在这里我们将显示关于附件url的信息。我不确定html模板到底是什么样子的,所以我做了一些通用的一个重要的标记,我们需要放在标记里面
createeditpayments.html:
class PaymentListForm(forms.Form):
Title = forms.CharField(max_length=50)
Receipt_Amount = forms.DecimalField(max_digits=8, decimal_places=2)
Request_Amount = forms.DecimalField(max_digits=8, decimal_places=2)
Receipt_Attachment = forms.FileField(label="", widget=forms.FileInput(
attrs={'style': "display: none",},
), )
def __init__(self, *args, **kwargs):
super(PaymentListForm, self).__init__(*args, **kwargs)
<form method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<span>Receipt attachment: </span>
<label for="id_Receipt_Attachment" class="mt-2 btn btn-primary " style="cursor: pointer;">Choose file...</label>
<span id="receipt_url"> {{ url_of_receipt }}</span>
<br><br>
<button class="btn btn-primary" role="button" type="submit">
Save
</button>
def edit_payments(request, trn_pk):
trns = get_transaction_by_id(trn_pk)
data = trns['data']
if request.method == 'GET':
form = PaymentListForm(initial=data)
return render(request, 'paymentlist/createeditpayments.html', {'form': form, 'url_of_receipt': data['Receipt_Attachment']})
我假设数据
是一个结构如下的字典:
data = {
'Title': 'Example title',
'Receipt_Amount': 21.99,
'Request_Amount': 34,
'Receipt_Attachment': 'media/example.receipt.pdf'
}
现在我们有了这样的输出:
最后一件事是点击标签后打开上传文件。我认为最好的方法是使用jQuery和短脚本。因此,我在createeditpayments.html的底部放置以下代码:
createditpayments.html
{% block javascript %}
<script>
// fetch url from hidden FieldField and insert it next to upload button
$("#id_Receipt_Attachment").change(function () {
{#readURL(this);#}
$('#receipt_url').text(' ' + this.files[0].name);
});
</script>
{% endblock %}
现在标签应该上传文件并更改旁边的url。
恐怕这是不可能的,请参见