0

I'm trying to do passing a variable to form from view on Django.

Simply, what I want to do is something like this below.

# views.py

def tmp(request):
    if request.method == 'POST':
        f = tmpForm(request.POST)
        if f.is_valid():
        // something more here.

    else:
        // this will pass nothing to the form
        f = tmpForm()

        // want to pass variable/parameter to the form
        initial = '''{
                "hoge": "hogehoge"
        }'''
        f = tmpForm(initial) // should be something like this maybe?

    return render(request, 'tmpapp/tmp.html', {'form': f})
# forms.py

class tmpForm(forms.Form):

    // retrieve the variable/parameter and set `initial` or something like that

    json = forms.CharField(widget=forms.Textarea, label='', help_text='JSON formatted data here.', initial=
          textwrap.dedent(
               # '''{
               #      "hoge": "hogehoge"
               # }'''
               initial // so you can use the variable like this.
          )
     )

So, How can I implement this?

ー PupSoZeyDe ー
  • 1,082
  • 3
  • 14
  • 33

0 Answers0