django形式:默认初始值

时间:2015-08-11 07:59:36

标签: python django forms django-forms django-templates

我现在正在使用django Framework来建立筹款网站。

但是,我在网站上遇到了一些问题。 我在模特中制作了不同的课程。

我想输入表单默认值。

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

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

例如,

models.py

class Fundinfo(models.Model):
    price = model.PositiveIntegerField(null= False)

class Funding(models.Model):
    funding = models.PositiveIntegerField(null= False)
    info = models.ForeignKey(Fundinfo)

forms.py

class FundingForm(forms.models.ModelForm):
    class Meta:
        model = Funding
        fields = ('funding', )

Template&#39; funding.html&#39;

{% 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 %}

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

1 个答案:

答案 0 :(得分:0)

如果您想为表单字段提供一些初始值,请在表单中使用initial

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

这是你如何分配初始值以形成

的方式
相关问题