skip to Main Content

In a django template i have this js script:

        const formData = new FormData(form);

        formData.append('taglist', JSON.stringify(selectedTagIds));

        console.log(JSON.parse(formData.get('taglist')))
        console.log(formData.get('title_raw'))
        
        form.submit()

selectedTagIds is an array. The console logs it correctly. After submitting I can access every standard form field in the django view but not the "taglist". The field doesn’t exist in the form. When I try to print it i get "None". What am I doing wrong?

2

Answers


  1. If it isn’t a member in your form class, you can access it by calling

    j = json.loads(request.POST["taglist"])
    
    Login or Signup to reply.
  2. One easy way would be to add the stringified array in the value of a hidden input in the form to submit.

    const additionalInput = document.createElement("input")
    additionalInput.value = JSON.stringify(selectedTagIds)
    additionalInput.name = 'taglist'
    additionalInput.type = 'hidden'
    form.append(additionalInput)
    
    form.submit()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search