NoReversMatch for url with parameter

117 Views Asked by At

So i have a simple model and i want to display list of my recipes. So in urls.py i have following url:

urlpatterns = [
    url(
        regex=r'^$',
        view=views.RecipeListView.as_view(),
        name='list'
    ),
    url(
        regex=r'^(?P<name>[\w.@+-]+)/$',
        view=views.RecipeDetailView.as_view(),
        name='detail'
    ),
]

name is just a name of recipe it can be anything. And than at my list i have following to give url:

<div class="list-group">
    {% for recipe in recipe_list %}
      <a href="{% url 'recipes:detail' recipe.name %}" class="list-group-item">
        <h4 class="list-group-item-heading">{{ recipe.name }}</h4>
      </a>
    {% endfor %}
  </div>

so i am passing argument of name into detail view but i still get error NoReverseMatch at /recipes/

Reverse for 'detail' with arguments '(u'This is my test recipe',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'recipes/(?P[0-9A-Za-z._%+-]+)/$']

And i am not sure what i am doing wrong.

1

There are 1 best solutions below

2
On

You are passing a tuple as the name (u'This is my test recipe',), and you are passing it as a positional argument, where your url method expects a keyword argument.

The correct way therefore is:

<a href="{% url 'recipes:detail' name=recipe.name[0] %}">