template {% url } tag syntax

29 views
Skip to first unread message

Richard Belew

unread,
Feb 26, 2017, 10:01:59 PM2/26/17
to Django users

i have a hardwired URL in my template:

/djggApp/guest/{{ guest.guestId }}/update/

that successfully matches against this url pattern:

url(r'^guest/(\d+)/update$', login_required(views.GuestUpdate.as_view()), name='updateGuest'),


now i am trying to replace the template code with a more modern reverse lookup via a url tag:

{% url 'djggApp/guest' guest.guestId 'update'%}

but this generates a NoReverseMatch error

Reverse for 'djggApp/guest' with arguments '(1, u'update')' and keyword arguments '{}' not found. 0 pattern(s) tried: []


Richard Belew

unread,
Feb 26, 2017, 10:08:48 PM2/26/17
to Django users
i should also probably say that the url spec is in the djggApp's specific urls.py file, included in the project's urls.py

my primary reference is the url tag reference doc but i must not be composing its arguments correctly?

Carsten Fuchs

unread,
Feb 27, 2017, 2:50:11 AM2/27/17
to django...@googlegroups.com
Hi Richard,

Am 27.02.2017 um 04:01 schrieb Richard Belew:
> {%url 'djggApp/guest'guest.guestId 'update'%}
>
> but this generates a /NoReverseMatch/ error
>
> /Reverse for 'djggApp/guest' with arguments '(1, u'update')' and keyword
> arguments '{}' not found. 0 pattern(s) tried: []/


In the {% url %}, you use "update" as a parameter, so you should either
write

{% url 'djggApp:updateGuest' guest.guestId %}

or, for example

url(r'^guest/(\d+)/(?P<action>(update|something_else))$',
login_required(views.GuestUpdate.as_view()), name='updateGuest'),

(there are alternative ways for this, e.g. using \w+)
Note that your view must deal with the new parameter.

And in the template

{% url 'djggApp:updateGuest' guest.guestId 'update' %}

Best regards,
Carsten
Reply all
Reply to author
Forward
0 new messages