John DOee
John DOee

Reputation: 1

Edit form field value on template

I'm trying to edit the value of a form field inside a django template. I've tried a lot of stuff. I think the following is supposed to work:

{% with form.name.value="asdsad" %}

    {{form.name}}

{% endwith %}

But it presents the following error: u'with' expected at least one variable assignment

Nothing seems to work. What is the correct form of doing this?

Upvotes: 0

Views: 1915

Answers (1)

sagar
sagar

Reputation: 801

You might have to use jQuery. The form field can be referenced by id = id_fieldname. You can then change the attribute value by using $('#id_fieldname).attr("value", new_value).

Suppose I want to edit name field of a form,

<script>
    var name_object = $('#id_name');
    var current_name = name_object.val();
    var new_name = "new_name"
    name_object.attr('value',new_name);
</script>

The id of the input tag of the HTML is always id_fieldname. Here, fieldname is the name given in the model.

Upvotes: 1

Related Questions