Custom widget initial value

Hi, I have a custom widget :

class GooglePlaceWidget(widgets.Input):
    template_name = 'widgets/EntreEauGooglePlaceWidget.html'

    class Media:
        js = ('js/Widgets/EntreEauNewAutocomplete.js',)
        css = {
            'all': ('css/Widgets/customWidget.css',)
        }

    def get_context(self, name, value, attrs):
             context = super().get_context(name, value, attrs)
             context['widget']['value'] = value or 'Dynamic Default'
            #  context['widget']['custom_attribute'] = 'some_value'
             return context

In forms.py, I try to change the initial value

adresse = forms.CharField(widget=GooglePlaceWidget(), label="Adresse")

    def __init__(self,addr= None, *args, **kwargs):
        super(EntreEauForm, self).__init__(*args, **kwargs)  
        if addr is not None:
            self.fields['adresse'].initial = addr

If I replace my widget by the default InputText. it work.

My widget work properly so far.

What should be changed to make it work ?

If anyone need extra code from me. I can share everything

Please post the code for the view where you’re trying to create an instance of this form.

'ee':EditEntreEauForm(instance=eer_instance, addr="some value", prefix="ee"),

Is it what you was looking for ?

Please post the complete view.

class GetSpecificEERMarkerData(LoginRequiredMixin, View):
    template = "async/entreEau.html"
    
    def get(self, request, *args, **kwargs):
        
        id = request.GET['id']

        # Retrouver l'instance d'objet associer si l'objet est associer a la ville du client
        eer_instance = EntreeEau.objects.get(id = id, related_client = request.user.ville)

        
        context = {
            'ee':EditEntreEauForm(instance=eer_instance,addr= "some value", prefix="ee"),
            'ee_status': EntreEauStatusForm(prefix="sts"),
            'image': AddImageForm(prefix='image'),
            # 'key': settings.GOOGLE_KEY,
            
        }
        return render(request, self.template, context)

In the form, if a replace my custom widget with default Text Field widget, it works

In that case, we probably need to see the template for your widget (EntreEauGooglePlaceWidget.html) and possibly the associated JavaScript (EntreEauNewAutocomplete.js) as well.

Here is the html :

<div id="custom-google-autocomplete-widget" types="address" data-onload="CustomWidgetInit">
    <div class="custom-google-widget-container">
        <div class="custom-google-widget-input-container">
            <!-- Pour ajouter un icon dans la zone de recherche -->
            <button class="custom-autocomplete-icon" aria-hidden="false" role="presentation" tabindex="-1"></button>
            <input type="text" placeholder="Entrer une adresse..." name='{{ widget.name }}' id="prediction_ee" aria-autocomplete="list" autocomplete="off" role="combobox" aria-expanded="false" aria-haspopup="listbox" aria-label="recherche de lieu"{% include "django/forms/widgets/attrs.html" %}>
            <div id="custom-google-focus-ring" class="custom-google-focus-ring"></div>
        </div>
        <div class="custom-google-prediction-anchor">
            <div class="custom-google-dropdown" part="prediction-list">
                <ul id="custom-google-results" role="listbox" aria-label="predictions">
                </ul>
            </div>
        </div>
    </div>
</div>

Actually, all the js is deactivated and I can’t really share publicly the code atm.

I do not see a value attribute on your input element to render the value of the widget.

If you look at the input.html template in Django, you’ll see:

{% if widget.value != None %} value="{{ widget.value|stringformat:'s' }}"{% endif %}

You are absolutely right.

Thank you very much!

Now, the input show the value from the model instance but he doesn’t show the new value.

Maybe I’m wrong using initial property for that. I need to override the displayed with something else.

the following code work :

if addr is not None:
            self.fields['related_adresse'].widget.attrs['value'] = addr

is it the correct way to do?