MultiWidget not rendering

189 views
Skip to first unread message

Farhan Khan

unread,
Dec 19, 2016, 12:40:12 PM12/19/16
to Django users
Hi all!
I am trying to get a MultiWidget with two TextFields ,however I am not able to get anything to render. Here is my code.

from django import forms
from django.forms import widgets

class DateSelectorWidget(widgets.MultiWidget):
   
def __init__(self, attrs=None):
        _widgets
= (forms.TextInput(),
                    forms
.TextInput())
       
super(DateSelectorWidget, self).__init__(_widgets, attrs)

   
def format_output(self, rendered_widgets):
       
return ''.join(rendered_widgets)

class Fourteen(forms.Form):
    mywidget
= DateSelectorWidget()


In the shell I will do:

>>> fourteen = Fourteen()
>>> print(fourteen)

>>>

Nothing will render. I suspect that I need to manually render the HTML, but the documentation is fairly light on this topic.

Any assistance would be greatly appreciated!

Matthew Pava

unread,
Dec 19, 2016, 2:03:41 PM12/19/16
to django...@googlegroups.com

I’m not that familiar with MultiWidget, but it would seem that your print statement should be:

print(fourteen.mywidget)

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/86bebdb4-1d45-4612-84d1-456f86942709%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Farhan Khan

unread,
Dec 19, 2016, 2:13:17 PM12/19/16
to django...@googlegroups.com
Unfortunately that does not display.
In the template I had it as {{ fourteen }} and saw it was rendering everything but my custom widget.

--
Farhan Khan
PGP Fingerprint: 4A78 F071 5CB6 E771 B8D6 3910 F371 FE22 3B20 B21B

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hUmvw-mdaiY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Collin Anderson

unread,
Dec 23, 2016, 11:13:27 PM12/23/16
to Django users
Hi,

I think you want something like this.

class Fourteen(forms.Form):
    mywidget = forms.DateField(widget=DateSelectorWidget())

Collin

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 djang...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hUmvw-mdaiY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Farhan Khan

unread,
Dec 25, 2016, 10:56:49 PM12/25/16
to Django users
Hi all, I figured out how it works.

Using MultiWidget requires three components: MultiWidget, MultiValueField and a form.

Here is my example:

class ComplexMultiWidget(forms.MultiWidget):
   
def __init__(self, attrs=None):
        widgets
= (
            forms
.DateInput(),
            forms
.Select(choices=(('D', 'Days'),
                                 
('W', 'Weeks'),
                                 
('Y', 'Years'))),
       
)
       
super(ComplexMultiWidget, self).__init__(widgets, attrs)

   
def decompress(self, value):
       
if value:
            data
= value.split(',')
           
return [data[0], data[1]]
       
return [None, None, None]

   
def format_output(self, rendered_widgets):
       
return u'\n'.join(rendered_widgets)


class ComplexField(forms.MultiValueField):
   
def __init__(self, required=True, widget=None, label=None, initial=None):
        fields
= (
            forms
.DateField(widget=forms.DateInput),
            forms
.ChoiceField(choices=(('D', 'Days'),
                                       
('W', 'Weeks'),
                                       
('Y', 'Years'))),
       
)
       
super(ComplexField, self).__init__(fields, required,
                                           widget
, label, initial)

   
def compress(self, data_list):
       
if data_list:
           
return '%s,%s' % (data_list[0],''.join(data_list[1]))
       
return None

class ComplexFieldForm(forms.Form):
    field1
= ComplexField(widget=ComplexMultiWidget())

I took the solution from here: http://stackoverflow.com/a/2387330

I hope this help someone in the future!
Reply all
Reply to author
Forward
0 new messages