Keep selected option after form submission

1,688 views
Skip to first unread message

julio....@m2agro.com.br

unread,
Jun 9, 2015, 5:47:17 PM6/9/15
to django...@googlegroups.com
Hi,

I have the same problem of this guy (http://stackoverflow.com/questions/21884631/keeping-option-selected-after-form-is-submitted-in-django), but I do not want to render a field like Daniel Roseman said in the answer.

Here is the code:

<select id="{{ form.activity.auto_id }}" name="{{ form.activity.name }}"  data-size="10">
{% for id, name in form.activity.field.choices %}
{% if form.activity.value == id %}
<option value="{{ id }}" selected>{{ name }}</option>
{% else %}
<option value="{{ id }}">{{ name }}</option>
{% endif %}
{% endfor %}
</select>

Thanks in advance.

Vijay Khemlani

unread,
Jun 9, 2015, 8:03:07 PM6/9/15
to django...@googlegroups.com
It doesn't answer the question, but why don't you want to render the field automatically?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a6a2db7a-e4b0-4281-b6db-6f31009ac68d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Jun 9, 2015, 8:49:11 PM6/9/15
to django...@googlegroups.com

You can remove that entire pasted section of code and replace it with {{ form.activity }}, Django will render it with the behavior you described.

-James

julio....@m2agro.com.br

unread,
Jun 10, 2015, 1:13:41 PM6/10/15
to django...@googlegroups.com
I decided to use {{ form.activity }} instead of that code above, but it is a bit strange, it should work in that way.

Now, I'm using django-widget-tweaks (https://github.com/kmike/django-widget-tweaks) to add some attributes to selected tag and it's working.

The reason why I wasn't this to render automatically is because it gets less customizable. For example: How would you add an attribute for each option in that select field?
 

Vijay Khemlani

unread,
Jun 10, 2015, 1:31:17 PM6/10/15
to django...@googlegroups.com
Subclassing the django.forms.widgets.Select class and setting it as a widget for the form field.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

julio....@m2agro.com.br

unread,
Jun 10, 2015, 1:36:07 PM6/10/15
to django...@googlegroups.com
OK, I will check this out. Thank you very much!

James Schneider

unread,
Jun 10, 2015, 3:33:44 PM6/10/15
to django...@googlegroups.com
Depending on how crazy you need to get, you may be able to get away
with just styling the widget instances in the Form or ModelForm
definition: https://docs.djangoproject.com/en/dev/ref/forms/widgets/#customizing-widget-instances

If you do want something crazy, you can also subclass the Select
widget to override the rendering as Vijay mentioned. The link above
has information on how to do that.

Alternatively, you can place the {% if %} statement inline and remove
the duplication, although its a bit more cumbersome to look at:


<option value="{{ id }}" {% if form.activity.value == id %}selected{%
endif %}>{{ name }}</option>


Django handles the rendering of Select widgets (ie {{ form.activity
}}) directly in Python rather than passing it off to a template, but
the logic they use is similar to what you have specified in your
template code.

https://github.com/django/django/blob/1.8.2/django/forms/widgets.py#L508

-James
> https://groups.google.com/d/msgid/django-users/04dca7e9-4a8c-4717-b907-68f29f299127%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages