0

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.

HyperX
  • 1,121
  • 2
  • 22
  • 42
  • Your url's regex doesn't match spaces, nor should it. You should use a slug – Sayse Dec 19 '16 at 07:50
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Dec 19 '16 at 07:51

1 Answers1

0

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] %}">
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Hmm i tried like that but not really managed to get it working. My name is charfield(255 length). How should i then manage that url is like recipes/this-is-my-recipe ? – HyperX Dec 19 '16 at 07:42
  • 1
    Use a slug field and adjust your regular expression. – Burhan Khalid Dec 19 '16 at 09:15