I have a database which uses formset to help store data. How do i get the ‘value’ of the numbers of data in the template. Like example i got a queryset of {[Item 1],[ Item 2]}. How do i get the value of 2 in the template to tell me theres 2 items?
Thanks for the quick response. It’s not working for me. I got the undefined in the console. Is this correct?
let data = ('#rowAddition').total_form_count
console.log(data)
I tried adding print(deviceInterface.total_form_count) in my views, it says queryset dont have such stuff
That’s a method on the formset, not a JavaScript function on an element or a function on a model.
Mind giving me a example? Im pretty new to this formset and I cant find guide on youtube
So in re-reading your question and how you attempted to use the answer, I realized there are two different possible cases here, each with a different answer.
If you’re trying to obtain the number of forms in the template, while the template is being rendered on the server before it is sent to the browser, you should be able to put {{ my_formset.total_form_count }}
in your template. (That’s the same technique used to retrieve any attribute of an object in the template - this is not specific to formsets.)
Always keep in mind that the Django objects, attributes, and methods only exist on the server. They are not available in the browser. Once a template has finished being rendered and is returned to the browser, Django is done.
If you’re trying to obtain the number of forms in JavaScript in your browser, then you should be able to rely upon the forms-TOTAL_FORMS
element in the management form. (If you can’t, then you’ve got an issue with how you’re managing the management form.)
Ok, thank you very much for the detail explanation