Django form:默认初始值

2024-10-02 14:23:47 发布

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

我现在用django框架制作筹款网站。在

但是,我在现场遇到了一些问题。 我做了不同级别的模特。在

我想用默认值。在

 {{ form.funding }} ..... -> problem 
 (<input value="{{ funding.price }}">) -> want to same form of this. 

有关详细信息,请参阅模板。在

例如

在模型.py在

^{pr2}$

在表单.py在

class FundingForm(forms.models.ModelForm):
    class Meta:
        model = Funding
        fields = ('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 %}

在视图.py在

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
    })

Tags: oftopyformprojectidinputvalue
1条回答
网友
1楼 · 发布于 2024-10-02 14:23:47

如果您想给表单字段指定一些初始值,那么在表单中使用initial

Class MYform(forms.ModelForm):
    funding = forms.PositiveIntgerField(initial=10)

这就是你如何给表单赋值的方法

相关问题 更多 >