B
B
blackbb2018-08-31 15:53:09
Django
blackbb, 2018-08-31 15:53:09

How to set a value in a django form so that it doesn't get reset after an update?

There is a search form by country

class CityChoiceForm(forms.ModelForm):
    country_from = forms.ModelChoiceField(
        queryset=Country.objects.all(),
        label="Страна",
        required=False,
        widget=ModelSelect2Widget(
            model=Country,
            search_fields=['title__icontains'],
        )
    )

I take it out like this:
<div class="select_wrap">
  {{form.country_from}}
</div>

Now, when choosing a country, we go to the search result page, where is the same form, but again with an empty value and the person does not understand or forgets which country he has chosen, how to insert the default value of the selected country into the form?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vikholodov, 2018-08-31
@blackbb

CityChoiceForm(initial={})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question