javascript - Django ChoiceField - How to show the currently selected value? -


i have django choicefield called states defined in forms class.

class stateform(forms.form):      def __init__(self, *args, **kwargs):         super(stateform, self).__init__(*args, **kwargs)         self.fields['states'] = forms.choicefield(widget=forms.select,                                  choices=options_state)         self.initial['states'] = 'any' 

i set initial value 'any', since want dropdown 'any' default.

in views.py, get_context_data() function, add context that's sent html.

        context['state_form'] = stateform() 

in html, can render dropdown using django's special language

{{ state_form.states }} 

so far good. see dropdown in page, , default value 'any'. can click on select other values, @ point javascript code send http request based on form. reload page filtered data. able of done.

the problem once page reloads, dropdown still says 'any'. want dropdown show selected data. i'm not sure how.

i recommand store value in session/cookie , in javascript change default value of dropdown based on session/cookie information.

more cookies can find in django doc: https://docs.djangoproject.com/en/1.9/topics/http/sessions/


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -