No olvides que en las ciencias un problema casi siempre tiene diversas resoluciones, pero nosotros compartiremos lo más óptimo y mejor.
Solución:
creo CheckboxSelectMultiple
debería funcionar de acuerdo a su problema.
En tus forms.py
escribe el siguiente código:
from django import forms
class CountryForm(forms.Form):
OPTIONS = (
("AUT", "Austria"),
("DEU", "Germany"),
("NLD", "Neitherlands"),
)
Countries = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=OPTIONS)
En tus views.py
define la siguiente función:
def countries_view(request):
if request.method == 'POST':
form = CountryForm(request.POST)
if form.is_valid():
countries = form.cleaned_data.get('countries')
# do something with your results
else:
form = CountryForm
return render_to_response('render_country.html', 'form': form,
context_instance=RequestContext(request))
En tus render_country.html
:
Lo hice de esta manera:
formularios.py
class ChoiceForm(ModelForm):
class Meta:
model = YourModel
def __init__(self, *args, **kwargs):
super(ChoiceForm, self).__init__(*args, **kwargs)
self.fields['countries'] = ModelChoiceField(queryset=YourModel.objects.all()),
empty_label="Choose a countries",)
urls.py
from django.conf.urls.defaults import *
from django.views.generic import CreateView
from django.core.urlresolvers import reverse
urlpatterns = patterns('',
url(r'^$',CreateView.as_view(model=YourModel, get_success_url=lambda: reverse('model_countries'),
template_name='your_countries.html'), form_class=ChoiceForm, name='model_countries'),)
sus_países.html
Funciona bien en mi ejemplo. Si necesita algo más, ¡pregúnteme!
Con respecto a mi segunda pregunta, esta es la solución. Una clase de extensión:
from django import forms
from django.utils.encoding import force_unicode
from itertools import chain
from django.utils.html import escape, conditional_escape
class Select(forms.Select):
"""
A subclass of Select that adds the possibility to define additional
properties on options.
It works as Select, except that the ``choices`` parameter takes a list of
3 elements tuples containing ``(value, label, attrs)``, where ``attrs``
is a dict containing the additional attributes of the option.
"""
def render_options(self, choices, selected_choices):
def render_option(option_value, option_label, attrs):
option_value = force_unicode(option_value)
selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
attrs_html = []
for k, v in attrs.items():
attrs_html.append('%s="%s"' % (k, escape(v)))
if attrs_html:
attrs_html = " " + " ".join(attrs_html)
else:
attrs_html = ""
return u''.format(
escape(option_value), selected_html, attrs_html,
conditional_escape(force_unicode(option_label))
)
'''
return u'' % (
escape(option_value), selected_html, attrs_html,
conditional_escape(force_unicode(option_label)))
'''
# Normalize to strings.
selected_choices = set([force_unicode(v) for v in selected_choices])
output = []
for option_value, option_label, option_attrs in chain(self.choices, choices):
if isinstance(option_label, (list, tuple)):
output.append(u'')
else:
output.append(render_option(option_value, option_label,
option_attrs))
return u'n'.join(output)
class SelectMultiple(forms.SelectMultiple, Select):
pass
Ejemplo:
OPTIONS = [
["AUT", "Australia", 'selected':'selected', 'data-index':'1'],
["DEU", "Germany", 'selected':'selected'],
["NLD", "Neitherlands", 'selected':'selected'],
["USA", "United States", ]
]
Sección de Reseñas y Valoraciones
Si conservas algún reparo o disposición de renovar nuestro artículo puedes ejecutar una disquisición y con placer lo observaremos.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)