#26431: translate_url() creates an incorrect URL when optional named groups are
missing in the URL pattern
-------------------------------+------------------------------------
Reporter: jekka-ua | Owner: nobody
Type: Bug | Status: new
Component: Core (URLs) | Version: 1.9
Severity: Normal | Resolution:
Keywords: translate_url | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.6a7f38dd97f7d6b28777ddae0e12f632%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.