# Django forms | Passing "value" widget attribute.

**URL:** <https://forum.djangoproject.com/t/django-forms-passing-value-widget-attribute/27992>\
**Category:** Forms & APIs\
**Created:** [February 12, 2024, 12:41pm UTC](https://forum.djangoproject.com/t/django-forms-passing-value-widget-attribute/27992 "2024-02-12T12:41:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlauweStadTechnologi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/blauwestadtechnologi/32/15959_2.png) [@BlauweStadTechnologi](https://forum.djangoproject.com/u/BlauweStadTechnologi)\
**Post date:** [February 12, 2024, 12:41pm UTC](https://forum.djangoproject.com/t/django-forms-passing-value-widget-attribute/27992/1 "2024-02-12T12:41:37Z")

</div>

Morning all

So I have a Django form which work and submits the form data to my third party CRM as expected.

However, this form allows browsers to send feedback about the page with which the form is placed on.

Because of this, I have a hidden field (`HiddenInput(...)`) which passes the URL of the current page in the `value` attribute so we know which page the user is referring to. The URL is then to be seen on our end as part of the message body.

This is how I have the `value` widget set up.

File name: `legal.forms.py`

```auto
    url = forms.CharField(widget=forms.HiddenInput(attrs={'value' : '{{urll}}'}))  

```

This is the equivalent of:

```auto
<input type = "hidden" value="<insert value>"..../>

```

the `url` context variable is attained by the following function:

Filename ‘legal.context\_processors.py’

```auto
def legal__url_path (request):
    url = request.get_host() + request.path 
    return {'url':url}

```

This function has been declared in the list of context processors in the `settings.py` in the following fashion:

```auto
'OPTIONS': {
            'context_processors': [
                .....
                .....
                .....
                'legal.context_processors.legal__url_path',
                .....
                .....
                .....
            ],

```

I have read the official documentation which can be found here[The Forms API](https://docs.djangoproject.com/en/5.0/ref/forms/api/) & I have to say, it’s as clear as mud.

I don’t understand why the following correctly passes the `url` in the ‘value’ attribute, as follows:

‘’

With the result being:

 ![Screenshot 2024-02-12 122654](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/c/7/c72d1a029345f4c6ebb3b278ae04d76a0e44213f.png)

However, doing it this way:

`…widget=forms.HiddenInput(attrs={‘value’ : ‘{{urll}}’})…’

returns the literal string, like this?

![Screenshot 2024-02-12 122710](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/e/c/ec34bc91ba21a807a9b4d0deacc4a0b5098bb7da.png)

**To summarise**

The in the image below, the top `input` is constructed via Django, the bottom is achieved via the standard HTML methodology:

![value-attr](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/b/c/bc6944294162beb64eb81d0c86a03ed847579c0a.png)

What am I missing here exactly?

Thank you.

---

<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:** [February 12, 2024, 12:53pm UTC](https://forum.djangoproject.com/t/django-forms-passing-value-widget-attribute/27992/2 "2024-02-12T12:53:47Z")

</div>

Because this:  
`url = forms.CharField(widget=forms.HiddenInput(attrs={'value' : '{{urll}}'}))`

Defines a literal value to be used as an attribute for that field. The `attrs` dict contains values that are interpolated into the template being rendered, the value itself is not interpreted during the rendering process.

To dynamically set values for fields in a form, you pass the values to the form constructor using the `initial` parameter in the view where the form is being used. See the docs and examples for [Initial form values](https://docs.djangoproject.com/en/5.0/ref/forms/api/#initial-form-values).

Now, if you’re setting this in a context processor because you’re doing this in multiple locations, then you’re likely going to want to render that directly as a separate template. The context processor is functioning at a separate point in time from when the form is being processed.

You may need to render that tag manually within your form and not as part of the Django form.

For example:

```auto
<form>
  {{ my_form}}
  {% include 'url_field.html' %}
</form>

```

Where URL field is your template that renders what the context processor adds to the context.

[Edit: This also implies that either: You don’t have `url` as a form field - which means you would need to explicitly pull that from the POST data in the view that processes the field, _or_ you render all-but-this-field manually so that this field doesn’t get rendered twice. But my actual recommendation would depend upon how many different forms you’re needing to do this with. If it’s only one or two, I would suggest removing the context processor and doing it within the view.]

---

<div class="post-metadata">

**Author:** ![BlauweStadTechnologi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/blauwestadtechnologi/32/15959_2.png) [@BlauweStadTechnologi](https://forum.djangoproject.com/u/BlauweStadTechnologi)\
**Post date:** [February 12, 2024, 1:19pm UTC](https://forum.djangoproject.com/t/django-forms-passing-value-widget-attribute/27992/3 "2024-02-12T13:19:41Z")

</div>

Great, I’m just going to include this as a separate field using the HTML methodology., in the following fashion:

```auto
<form method="POST"> 
            {% csrf_token %}

            {% for hidden in form.hidden_fields %}
            {{ hidden }}
            {% endfor %}

            {% for field in form.visible_fields %}
            <div class="form-group">
                {{ field.errors }}
                <label>{{ field.label_tag }}</label>
                <p>{{ field }}</p>
            </div>
            {% endfor %}
            <div class="disclaimer">
                {% include 'snippets/privacy-notice.html' %}
            </div>
            <input type="hidden" name="url" value="{{url}}"/>
            <button type="submit" class="btn btn--pink">Submit Feedback</button>
        </form>

```

I work on the **It’ll either be easy or it’s not happening** principle. Django just complicates this more than it needs to be IMHO, & I can’t deal with it right now.

Whoever writes this documentation just seems to assume we’re all experts in Django.

Thank you for your time.

---

<div class="post-metadata">

**Author:** ![BlauweStadTechnologi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/blauwestadtechnologi/32/15959_2.png) [@BlauweStadTechnologi](https://forum.djangoproject.com/u/BlauweStadTechnologi)\
**Post date:** [February 12, 2024, 1:23pm UTC](https://forum.djangoproject.com/t/django-forms-passing-value-widget-attribute/27992/4 "2024-02-12T13:23:36Z")

</div>

On a personal note, how have I come across this time around? Better? Worse? Calm? 🙂

(Not sarcasm 😊)
