Opened 7 years ago

Closed 4 years ago

#6388 closed Bug (wontfix)

as_p produce invalid HTML for RadioSelect

Reported by: batiste Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords:
Cc: simonluijk@…, m@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

An ul element must not exist in a p element. This produce some CSS problem because rules like this will not be applied by Mozilla/Konqueror : p ul {color:red}

http://code.djangoproject.com/browser/django/trunk/tests/regressiontests/forms/regressions.py#L43

Change History (12)

comment:1 Changed 7 years ago by anonymous

  • Component changed from Uncategorized to django.newforms
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by mattmcc

  • Summary changed from as_p produce invalid HTML for ChoiceField to as_p produce invalid HTML for RadioSelect

comment:3 Changed 7 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 5 years ago by erikr

  • Needs tests set
  • Owner changed from nobody to erikr
  • Status changed from new to assigned

comment:5 Changed 5 years ago by erikr

The difficulty here is that as_p() wraps the <p></p> around, so the only proper option I see is to stop using <ul> in radioboxes.
However, this would break backwards compatibility, e.g. people styling these lists with CSS.

comment:6 Changed 5 years ago by erikr

  • Owner changed from erikr to nobody
  • Status changed from assigned to new

comment:7 Changed 4 years ago by simonluijk

  • Cc simonluijk@… added
  • Triage Stage changed from Accepted to Design decision needed

A decision needs to be made in regards to backwards compatibility. So moving to DDN.

I am new to this so if this is wrong please slap me gently!

comment:8 Changed 4 years ago by JMagnusson

  • Cc m@… added

comment:9 Changed 4 years ago by ramiro

#14776 reported this again.

Fortunately, there exists a RadioFieldRenderer helper class for the RadioSelect widget that is in charge of its rendering as a whole and can choose a (variation of) RadioInput for rendering each radio select <INPUT>. RadioSelect() accepts a 'renderer' argument. The key method is .render(). E.g.:

from django.forms import RadioFieldRenderer, RadioSelect

class MyRFRenderer(RadioFieldRenderer):
    def render():
        return mark_safe(u'<div id=blah>\n%s</div>\n' % u'\n'.join([u'%s'% force_unicode(w) for w in self]) + u'\n')

...

class MyModelForm(ModelForm):
    some_field = forms.ModelChoiceField(queryset=..., renderer=MyRFRenderer)
    Meta:
        model = MyModel

So fixing this (and possibly #13401, although it is a bit more convoluted) is already possible for developers.

If we are going to fix in Django the problem is the mechanism of backward compatibility and/or gradual deprecation+ warning to use.

comment:10 Changed 4 years ago by batiste

I like this approach to form rendering:

https://github.com/brutasse/django-floppyforms

Django could have some templates called form_as_p.html and form_as_table.html that people could override?

comment:11 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

Also see #15667.

comment:12 Changed 4 years ago by carljm

  • Easy pickings unset
  • Resolution set to wontfix
  • Status changed from new to closed
  • UI/UX unset

This is a minor bug and there's no reasonable backwards-compatible way around it. As_p is going to continue to render with paragraph tags, and UL is the logical default markup for a radio select. The fix is to not use as_p if you have a radio select field, or to customize your radio field renderer as documented above (and hopefully soon we'll have an even better way with #15667). Methods like as_p are just for quick prototyping - if you care about your markup, render your form in your own templates.

Note: See TracTickets for help on using tickets.
Back to Top