city selection in region/city contextual dropdown not showing after selection
pmeleney opened this issue · comments
Goal
Show the selected city in region/city contextual dropdown
Problem
The city remains the original value of "--------" even after selection. The back end capturing the selected city so something is working, but the city name is not showing in the selection dropdown.
Code Snippet
accounts.forms.py
class ProfileCreationForm(UserCreationForm, ModelSelect2Widget):
location_region = forms.ModelChoiceField(queryset=USRegion.objects.all().order_by('name'), label="US Region (optional)", required=False)#widget=ModelSelect2Widget(model=USRegion, search_fields=['name__istartswith'])
location_city = forms.ModelChoiceField(queryset=USCity.objects.all().order_by('name'), label="US City (optional)", required=False, widget=ModelSelect2Widget(model=USCity, dependent_fields={'location_region':'region_id'}, search_fields=['name__istartswith'], max_results=100))
phone_number = forms.CharField(max_length = 32, required=False, label = "Phone number (optional)")
first_name = forms.CharField(max_length = 255, required=True, help_text="Required.")
last_name = forms.CharField(max_length=255, required=True, help_text="Required.")
email = forms.EmailField(required=True, help_text="Required.")
promo_code = forms.CharField(max_length=255, required=False, label="Promotional Code (optional)")
Hey @pmeleney,
Thanks for reaching out. I am sure we can find a solution. I am really glad you attached a screenshot. Just by looking at it, I can see that the second input seems to be a regular select input and not a Select2-JavaScript one.
Are you by any chance adding the City input dynamically after the state is selected? In that case, you need to initialize the select2 input first. You simply need to call execute the following JS code, after insert the select-tag into the DOM:
$your_select_element.djangoSelect2()
Please let me know, should this not solve the issue or ask any question you like.
Best
-Joe
Awesome. Works like a charm. Thanks so much!