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

On Wed, Jun 10, 2015 at 10:36 AM,  <julio.lace...@m2agro.com.br> wrote:
> OK, I will check this out. Thank you very much!
>
> Em quarta-feira, 10 de junho de 2015 14:31:17 UTC-3, Vijay Khemlani
> escreveu:
>>
>> Subclassing the django.forms.widgets.Select class and setting it as a
>> widget for the form field.
>>
>> On Wed, Jun 10, 2015 at 2:13 PM, <julio....@m2agro.com.br> wrote:
>>>
>>> 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?
>>>
>>>
>>> --
>>> 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/1ec396fb-ff4b-43ea-8304-4a876bd56c35%40googlegroups.com.
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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/04dca7e9-4a8c-4717-b907-68f29f299127%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CA%2Be%2BciWHiwo%3DW64AnWDWSan06yB1N1XE7mFAJYp50CR4-eGuHw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to