--
Ticket URL: <https://code.djangoproject.com/ticket/26431>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "patch-stable-1.9.txt" added.
* Attachment "patch-master.txt" added.
* needs_better_patch: => 1
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0
Comment:
Could you please provide the patch as a pull request against master?
Unless this is a regression, it probably doesn't qualify for a backport to
1.9 (see our [https://docs.djangoproject.com/en/dev/internals/release-
process/#supported-versions supported versions policy]). Please check the
patch on Python 3 as well (`dict.iteritems()` doesn't exist there).
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:1>
Comment (by knbk):
The issue here is that `resolve()` will return `None` for any optional
capture groups that aren't captured, but `reverse()` will use
`force_text()` and convert it to the literal string `'None'`. This causes
an asymmetry between `resolve()` and `reverse()`.
I think it's a better idea to treat this asymmetry as a bug, and solve it
within `reverse()`. We would discard any arguments that are `None`.
Incidentally this will allow for cleaner code when you don't know if an
optional parameter is set when you call `reverse()` or the `{% url %}`
template tag:
{{{
{% if obj.might_be_none != None %}
{% url 'my_view' obj.might_be_none %}
{% else %}
{% url 'my_view' %}
{% endif %}
}}}
vs.
{{{
{% url 'my_view' obj.might_be_none %}
}}}
I think that's a reasonably common usecase, so it would be nice to support
this cleaner syntax. Non-existent template variables will still pass an
empty string rather than `None`, so this won't hide any typos in your
template variable names.
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:2>
* owner: nobody => Daniel Rios
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:3>
Comment (by j-bernard):
This also affects the set_language function.
When changing language on a page with an URL pattern containing an
optional group, the empty optional part is set to none and translating the
page leads to a 404.
The bug on translation did not exist back in 1.8, not sure when it was
introduced. I'm on 2.2.
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:4>
* needs_better_patch: 1 => 0
* version: 1.9 => master
Comment:
[https://github.com/django/django/pull/11477 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:5>
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:6>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"76b993a117b61c41584e95149a67d8a1e9f49dd1" 76b993a]:
{{{
#!CommitTicketReference repository=""
revision="76b993a117b61c41584e95149a67d8a1e9f49dd1"
Fixed #26431 -- Prevented django.urls.resolve() from returning missing
optional parameters.
Previous behavior was inconsistent with django.urls.reverse() and
caused that translate_url() created an incorrect URL when an optional
parameter was missing.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:8>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"d640c71fa35640b4c13107c074be2f52c52ad861" d640c71f]:
{{{
#!CommitTicketReference repository=""
revision="d640c71fa35640b4c13107c074be2f52c52ad861"
Refs #26431 -- Added tests for resolving URL and translate_url() with
provided optional parameter.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:7>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"6cb30414bc0f83b49afc4cae76d4af5656effe9a" 6cb30414]:
{{{
#!CommitTicketReference repository=""
revision="6cb30414bc0f83b49afc4cae76d4af5656effe9a"
[3.0.x] Fixed #31069, Refs #26431 -- Doc'd RegexPattern behavior change in
passing optional named groups in Django 3.0.
Backport of 9736137cdc4b7528a0aca17415dc9798660eed81 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:9>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"9736137cdc4b7528a0aca17415dc9798660eed81" 9736137]:
{{{
#!CommitTicketReference repository=""
revision="9736137cdc4b7528a0aca17415dc9798660eed81"
Fixed #31069, Refs #26431 -- Doc'd RegexPattern behavior change in passing
optional named groups in Django 3.0.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26431#comment:10>