开发者

How to get the order value of a form within template (django)?

I am using formset for my project. I have several form in my formset. Now I want to customize the appearance of form. I want to do this by using the order value of each form. One example of the input for an "ORDER" of form is shown below:

<input type="text" name="phones-0-ORDER" value="1" id="id_phones-0-ORDER">

I want to get the value(value="1" in this case) of this input.

I have generated the formset from my models directly using the inlineformset_factory in my view. https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-开发者_StackOverflow社区an-inline-formset-in-a-view

At the creation of my formset, I have used the following code:

PhoneNumberFormSet = inlineformset_factory(Patron, PhoneNumber, can_order=True)

In this way, every form in the formset will have an order. Let's say we have 3 forms in the formset, the first form will hold the order 1, the second order 2, the third order 3. I want to use the "order" of the form in my template to control a loop.

Anyone knows how to get the order value in template? For your information, the type of ORDER is IntegerField. So my question is equal to "how to get the initial(pre filled-in) data of an IntegerField in template.

Thanks for your answers!


I believe you are asking how to set initial data within a formset. If that is the case, you will find the following information valuable within the django docs:

https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset

If you are actually asking about how to get to the values from the client side, you would want to do so with jquery selectors.

http://api.jquery.com/category/selectors/


{% if form.can_order %}
    {{ form.ORDER }}
{% endif %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜