0

I am using this post as a reference: Django: Adding inline formset rows without javascript

if request.method=='POST':
  PrimaryFunctionFormSet = inlineformset_factory(Position,Function)
  if 'add' in request.POST:
    cp = request.POST.copy()
    cp['prim-TOTAL_FORMS'] = int(cp['prim-TOTAL_FORMS'])+ 1
    prims = PrimaryFunctionFormSet(cp,prefix='prim')

I am trying to add inlines rows without javascript and came across a few things I didn't understand in the implementation referenced above.

  1. How do you get a button called 'add' in your form or view or template?
  2. 'prims' was defined in the form but 'prim-TOTAL_FORMS' was not. Do you need to define 'prim-TOTAL_FORMS' somewhere?
  3. How can you call 'prim-TOTAL_FORMS' before you defined 'prim'?
  4. Is this all writtein in the views.py?

Thanks for the help, and sorry if the questions are novice!

1 Answer 1

1

a) Add in a button with a name and value you wish, and it will be submitted as POST data, with the key in the POST being the name, and the value the value.

<input type="submit" value="true" name="add">

Read up on HTTPRequest objects in Django here.

b) No. The example uses an inlineformset_factory. This sets the TOTAL_FORMS value for you on POSTing, using the given prefix 'prim'. They are taking a copy of the current TOTAL_FORMS count, and adding 1, then returning a new formset.

c) prim is defined in the initial formset, and you are ensuring you are returning a new one with the same prefix. This code is if the form is submitted, so you know 'prim' has been set to the prefix.

d) Yes!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.