1

I'm quite new to this Django stuff and i'm getting a NoReverseMatch at /cityinfo/

Exception Value:

Reverse for 'user_favorites' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['cityinfo/(?P<fav_id>[0-9]+)/$']

basically what i'm trying to do is get all the users Favorited posts and display them when the user clicks on the favorite navigation link in base.html

base.html

                <li class="#">
                    <a href="{% url 'cityinfo:user_favorites' favorites.id %}">
                        <span class="glyphicon glyphicon-floppy-disk"></span>&nbsp; Favourites
                    </a>
                </li>

urls.py

url(r'^(?P<fav_id>[0-9]+)/$', views.user_favorites, name="user_favorites"),

views.py

def user_favorites(request, fav_id):
if not request.user.is_authenticated():
    return render(request, 'cityinfo/login.html')
else:
    favorites = get_object_or_404(user_favourite_spot, id=fav_id)
    context = {
        "favorites": favorites
    }
    return render(request, 'cityinfo/user_favorites.html', context)

appreciate your help

  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Oct 30 '17 at 07:13
  • I'm guessing you're trying to call it from a different view, your `favorites` doesn't exist in the current context ( or its id is None) – Sayse Oct 30 '17 at 07:15

1 Answers1

4

You have no fav_id in your template's context, so when the template renders the variable, it's rendering to ''.

Change your url tag to {% url 'cityinfo:user_favorites' favourites.id %}

dannosaur
  • 2,519
  • 1
  • 16
  • 15