[WIP] Fixed #15667 -- Added template-based widget rendering.#4848
[WIP] Fixed #15667 -- Added template-based widget rendering.#4848prestontimmons wants to merge 1 commit intodjango:masterfrom prestontimmons:ticket-15667
Conversation
django/forms/forms.py
Outdated
There was a problem hiding this comment.
Do we actually need to pass in the renderer separately to BoundField? It gets a reference to the whole form already, so it could just access form.renderer.
|
This looks really promising! I didn't have time to carefully review all the widget and test changes, but I reviewed the core API code. |
|
awsome. any plan for jinja2 based rendering? |
|
I pushed some updates to the test suite. My first goal to get all the widget tests working again is complete. The old widget tests are doc tests masquerading as unit tests. They include a ton of duplicate testing for unicode handling, attrs overriding, etc. I started to rectify this by splitting tests for each widget into separate test cases and files. These now live in My second goal to make the rest of My next goals are to write tests for the new API changes and to convert the leftover widgets that still use string rendering. |
|
@auvipy jinja2 will be the default renderer. I find this patch to be a bit easier to start with Django templates first, though. |
|
Hi Preston, Great work so far as usual! One question I had is if you considered other template location namespace than |
|
This would also allow us to put form rendering ( |
|
I think keeping the widget rendering as unchanged as possible in this patch is smart - HTML5 required attributes are a good idea IMO, but there's a separate ticket for that and it can be a separate commit: https://code.djangoproject.com/ticket/22383 The widget tests look much improved, but I'd also argue for keeping those tests as unchanged as possible in this PR (even if they're bad) and fix them in a follow-up commit. It's much easier to review and verify a big refactor if you can trust the tests to be mostly unchanged and not have to review them carefully too at the same time. Are the test changes necessary to get them passing with the HTML-rendered widgets, or could those test changes possibly be pulled out and moved to a separate follow-up PR? I also slightly prefer @charettes Great point about converting |
|
Regarding the tests, another good option could be to pull out the test changes and commit those first, rather than second, if that's easier. Either way, the benefit of splitting the bulk of the test changes from the rendering changes is that we get a clean test run in between them, which goes a long way towards avoiding unexpected and undesired changes in the output. That said, if it's really hard to do, it's not strictly necessary - just means we have to review extra carefully :-) |
|
@charettes I didn't think through the namespace that much. @carljm Since the most recent implementation of |
|
what about dtlform for dtl based renderer and jinjaform? |
|
@auvipy The DTL and Jinja templates will be in |
|
It's not pushed yet, but I now have most of the widgets updated to use templates. This includes some of the tricky ones, like The leftover widgets all use Do you think it's sufficient to remove https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.RadioSelect Additionally, I could move the |
|
@prestontimmons I don't think it's feasible to worry about undocumented internal APIs when making a change like this, even if we have some evidence that some people might be using it. If it's undocumented, we've made no guarantees about its future. Just get rid of it (probably give it a mention in the release notes, but that's all). The documented iteration-over-BoundField behavior, on the other hand, needs to stay, at least for a deprecation path. If the code to support it isn't too bad, I don't really see why it shouldn't stay permanently; it's a handy shortcut that in many cases will still be simpler than defining your own full-blown radio-select template. I guess the issue with supporting is that it will mean either duplicating the individual option markup, or using an include inside the options loop of the radio-select etc templates, which could hurt performance? |
|
Cool. :) Iterating over the options is easy. It's the I'll play around with it some and see what I find. |
|
Now that I take a closer look at it I think it will be easy to do. |
|
There's a lot of work left to do, but I have The tricky part now is finding consistent behavior for the Unlike To get around that, the existing Unlike
The iteration API has further inconsistency in the templates. For instance, Further, because there are two render methods, supporting this API requires either two templates for each option class, or an extra context argument like The approach I took in the current patch is to:
Another option is to make Finally, there's some inconsistency I don't like in the templates. The following templates are mutually exclusive:
Preferably, the parent widget templates would reuse the iteration API and call Is this a useful feature? It looks like it only exists for use in the test suite. It's not documented as far as I can tell. |
|
Good news. I found a better API I'm stoked about.
Templates access values from the That means widgets like Where
I think I can focus on writing tests and documentation now. |
There was a problem hiding this comment.
If you want to prevent whitespace in jinja2 you can use {%- tag %} or {% tag -%}.
Then you can use indentation in the templates without outputting the whitespace.
See also: http://jinja.pocoo.org/docs/dev/templates/#whitespace-control
|
@timgraham Getting build failures on Python 3.4 in this branch: It seems related to using the unittest.subTest context manager in Any clue why Jenkins would fail on that? I can remove the |
|
Looks like xmlrunner/unittest-xml-reporting#85 |
|
@timograham Can I get your thoughts on the easiest way to commit the tests in this branch? You'll see in I see a few options:
I'm leaning toward the last option, but I'm happy to go either way if anyone has better ideas. |
|
The last option is what I would do too. |
|
Added PR #5209 for the test changes. |
tests/admin_widgets/tests.py
Outdated
There was a problem hiding this comment.
Read the warning 2 lines above :-)
|
Hi all, I rebased this patch onto master. Tests are passing again. The branch is close to feature complete, but the docs for it are still a big job. For anyone interested, here are the steps I believe are left to be taken: Templates
Docs
Bugs
Optimizations (optional)
|
|
Rebased PR is #6498. This branch is on django/django so anyone can add commits to it. I'll make sure all the comments here are addressed before that gets merged (which is probably at least a week away depending on who else pitches in). |
No description provided.