Python django表单:默认初始值
我现在正在使用django框架构建筹款网站 但是,我在现场遇到了一些问题。 我做了不同的模型课 我想在表单中输入默认值Python django表单:默认初始值,python,django,forms,django-forms,django-templates,Python,Django,Forms,Django Forms,Django Templates,我现在正在使用django框架构建筹款网站 但是,我在现场遇到了一些问题。 我做了不同的模型课 我想在表单中输入默认值 {{ form.funding }} ..... -> problem (<input value="{{ funding.price }}">) -> want to same form of this. forms.py class FundingForm(forms.models.ModelForm): class Meta:
{{ form.funding }} ..... -> problem
(<input value="{{ funding.price }}">) -> want to same form of this.
forms.py
class FundingForm(forms.models.ModelForm):
class Meta:
model = Funding
fields = ('funding', )
模板'funding.html'
{% for funding in fundings %}
<h3>{{ funding.price }}or more</h3>
<form method= "post" action="">
{{ form.funding }} ..... -> problem
(<input value="{{ funding.price }}">) -> want to same form of this.
{% csrf_token %}
{% if form.errors %}
<div class = "help-block">{{ form.funding.errors }}</div>
{% endif %}
<button id = "" type = "submit">funding</button>
</form>
{% endfor %}
如果要为表单字段提供一些初始值,请在表单中使用
initial
Class MYform(forms.ModelForm):
funding = forms.PositiveIntgerField(initial=10)
这就是如何为表单分配初始值的方法您遇到的问题是什么?这里没有问题。有一件事:您编写了
ForeignKey
,它应该是ForeignKey
。我想输入默认的表单值。与form=FundingForm(initial={'funding':form.funding.price})相同,但我必须输入模板。。
def view_funding(reqeust, project_id):
fundings = FundInfo.objects.filter(project= project_id)
form = PledgeForm() # How to use in here form = FundingForm(initial={'funding': form.funding.price})
return render(request, 'funding.html', {
'fundings': info,
'form': form
})
Class MYform(forms.ModelForm):
funding = forms.PositiveIntgerField(initial=10)