K G
K G

Reputation: 1815

How to use a reverse url lookup with argument in Django?

I have the following URL config in my Django project:

urlpatterns = [
    path('', views.record_create),
    path('<entry_method>/', views.record_create, name = "record_create"),
]

The view goes:

def record_create(request, entry_method="bulk/"):

In my template I do:

<a href="{% url "record_create" entry_method=request.path_info %}">

And I get the following error:

django.urls.exceptions.NoReverseMatch: Reverse for 'record_create' with keyword arguments '{'entry_method': '/bulk/'}' not found. 1 pattern(s) tried: ['(?P<entry_method>[^/]+)/$']

Not sure what I'm doing wrong. Any suggestions are welcome.

Upvotes: 1

Views: 113

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477190

The problem is not in the reverse lookup. The problem is that your entry_method in the url patterns, is by default a str path converter, not a path. You can set the type of the parameter with:

urlpatterns = [
    path('', views.record_create),
    path('<path:entry_method>', views.record_create, name = "record_create"),
]

For more information, see the documentation on Path converters [Django-doc].

Upvotes: 1

Related Questions