# Custom widget initial value

**URL:** <https://forum.djangoproject.com/t/custom-widget-initial-value/41016>\
**Category:** Forms & APIs\
**Created:** [May 20, 2025, 2:28pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016 "2025-05-20T14:28:46Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 2:28pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/1 "2025-05-20T14:28:46Z")

</div>

Hi, I have a custom widget :

```auto
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

```auto
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 ?

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 3:00pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/2 "2025-05-20T15:00:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 20, 2025, 3:02pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/3 "2025-05-20T15:02:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 3:04pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/4 "2025-05-20T15:04:19Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 3:43pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/5 "2025-05-20T15:43:11Z")

</div>

Is it what you was looking for ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 20, 2025, 3:44pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/6 "2025-05-20T15:44:50Z")

</div>

Please post the complete view.

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 3:47pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/7 "2025-05-20T15:47:18Z")

</div>

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 3:48pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/8 "2025-05-20T15:48:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 20, 2025, 3:56pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/9 "2025-05-20T15:56:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 4:04pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/10 "2025-05-20T16:04:33Z")

</div>

Here is the html :

```auto
<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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 20, 2025, 4:58pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/11 "2025-05-20T16:58:00Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 5:22pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/12 "2025-05-20T17:22:20Z")

</div>

You are absolutely right.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 5:32pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/13 "2025-05-20T17:32:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![technologiesflorizon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/technologiesflorizon/32/27571_2.png) [@technologiesflorizon](https://forum.djangoproject.com/u/technologiesflorizon)\
**Post date:** [May 20, 2025, 5:36pm UTC](https://forum.djangoproject.com/t/custom-widget-initial-value/41016/14 "2025-05-20T17:36:08Z")

</div>

the following code work :

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

```

is it the correct way to do?
