Opened 4 months ago

Closed 4 months ago

#35616 closed Uncategorized (invalid)

"url" template tag works with a context variable only if left unquoted

Reported by: Petr Boháč Owned by:
Component: Template system Version: 5.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I tried passing a context variable as the url_name parameter like so:

{% url 'url' %}

Django raises a NoReverseMatch exception - Reverse for 'url' not found. 'url' is not a valid view function or pattern name.

When left unquoted like so:

{% url url %}

PyCharm gives me a warning (View names should be in quotes since Django 1.5 and should have no quotes before 1.5) despite Django apparently accepting this and resolving to the correct URI.

Using Django 5.0.7

Change History (1)

comment:1 by Sarah Boyce, 4 months ago

Resolution: invalid
Status: newclosed

Hi Petr, in the documentation for url, there is a warning:

Don’t forget to put quotes around the URL pattern name, otherwise the value will be interpreted as a context variable!

In this case, you want it to be treated as a context variable and so not adding quotes is correct and adding them treats it as a view name.
This is working as expected to me and the behaviour is documented. Please feel free to give feedback to PyCharm 👍

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