Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#16308 closed Uncategorized (worksforme)

CheckboxSelectMultiple can not show the data on the view

Reported by: jekyllhy3@… Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords: Checkbox Select Multiple
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by julien)

CheckboxSelectMultiple can not show the data on the view.

when the form use CheckboxSelectMultiple, it can not show the data on view.

problem code :

class CheckboxSelectMultiple(SelectMultiple):
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and 'id' in attrs
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<ul>']
        # Normalize to strings
        str_values = set([force_unicode(v) for v in value])


 str_values = set([force_unicode(v) for v in '''eval'''(value)])

Attachments (1) (16.6 KB) - added by jekyllhy3@… 3 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 3 years ago by julien

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to needsinfo
  • Status changed from new to closed

I've reformatted your description -- next time please use preview to make sure your code samples display correctly.

Using eval() is potentially dangerous as maliciously code could get executed. It is unclear what problem you're having. Please reopen this ticket if you can provide a clear example or a test case illustrating the problem that you're trying to solve.

Changed 3 years ago by jekyllhy3@…

comment:2 Changed 3 years ago by anonymous

I have upload a clear example to show this problem.
The example can be download by the attachment

comment:3 Changed 3 years ago by julien

  • Resolution needsinfo deleted
  • Status changed from closed to reopened

Reopening so that this ticket can be reviewed again (I don't have time to do it just now).

comment:4 Changed 3 years ago by BernhardEssl

  • Component changed from Documentation to Forms
  • Resolution set to worksforme
  • Status changed from reopened to closed

I had a look at your attached code and as I can see you missing the initial option.

You can use initial to select values on a choice field like this:

gender = forms.MultipleChoiceField(initial=['m'],required=False, widget=CheckboxSelectMultiple, choices=(('m', 'Male'), ('f', 'Female')))

this generates in the view:

u'<p>Gender: <ul>\n<li><label><input checked="checked" type="checkbox" name="gender" value="m" /> Male</label></li>\n<li><label><input type="checkbox" name="gender" value="f" /> Female</label></li>\n</ul></p>'

hope this helps!

comment:5 Changed 3 years ago by anonymous

I think you did not catch what i meen.
When you submit your form, the release version can not keep the selected option.
If the choice is (('m', 'Male'), ('f', 'Female')), the realse version will work.
If the choices is (('male', 'Male'), ('female', 'Female')), the realse version will not work.

comment:6 Changed 3 years ago by anonymous

Please submit the form, you will see the problem.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.