[Django] #34261: Use {% url model_object %} to work with get_absolute_url

6 views
Skip to first unread message

Django

unread,
Jan 15, 2023, 7:42:19 AM1/15/23
to django-...@googlegroups.com
#34261: Use {% url model_object %} to work with get_absolute_url
--------------------------------------------+------------------------
Reporter: Willem Van Onsem | Owner: nobody
Type: Uncategorized | Status: new
Component: Template system | Version: 4.1
Severity: Normal | Keywords: url
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
Django has a `{% url ... %}` tag to calculate a reverse url. The advantage
of this is that it raises an error in case the url can not be found, and
thus makes templates less buggy.m

An alternative is to define a `.get_absolute_url()` method on a model
object, and then use it in the template as `{{
model_object.get_absolute_url() }}`. If a model object however has no
`get_absolute_url` method, it will return an empty href, not an error.

We can combine the two and check if the first parameter has a
`get_absolute_url` method, if that is the case, we return the result of
`get_absolute_url` instead.

--
Ticket URL: <https://code.djangoproject.com/ticket/34261>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 16, 2023, 12:12:31 AM1/16/23
to django-...@googlegroups.com
#34261: Use {% url model_object %} to work with get_absolute_url
----------------------------------+--------------------------------------

Reporter: Willem Van Onsem | Owner: nobody
Type: New feature | Status: closed

Component: Template system | Version: 4.1
Severity: Normal | Resolution: wontfix

Keywords: url | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: Uncategorized => New feature
* resolution: => wontfix


Comment:

Thanks for this ticket, however, IMO this syntax would be really confusing
for users as it mixes URL resolvers and model methods. It would also
unnecessarily complicate an existing Django code.

Please first start a discussion on the DevelopersMailingList, where you'll
reach a wider audience and see what other think, and
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features follow the guidelines with regards to
requesting features].

--
Ticket URL: <https://code.djangoproject.com/ticket/34261#comment:1>

Django

unread,
Jan 16, 2023, 2:03:42 AM1/16/23
to django-...@googlegroups.com
#34261: Use {% url model_object %} to work with get_absolute_url
----------------------------------+--------------------------------------
Reporter: Willem Van Onsem | Owner: nobody
Type: New feature | Status: closed
Component: Template system | Version: 4.1
Severity: Normal | Resolution: wontfix
Keywords: url | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Willem Van Onsem):

Well the confusion started for me... when `redirect` accepts a model
object, and `{% url ... %}` does not... :P

--
Ticket URL: <https://code.djangoproject.com/ticket/34261#comment:2>

Reply all
Reply to author
Forward
0 new messages