Reverse for 'album_details' with arguments '('',)' not found. 1 pattern(s) tried: ['client/user/(?P<pk>[0-9]+)/$']

Hello everyone. I have an app where users can see album and photos, but they have to be authenticated to be able to do that. In views.py

@login_required(login_url='login')
def index(request):
    if request.user.is_authenticated:
        user = request.user
        username = request.user.pk
        album = Album.objects.filter(owner_id=username)
        contexts = {
            'album': album,
            'user': user
        }
        return render(request, 'client/index.html', contexts)
    else:
        return render(request, 'client/login.html')

index.html

{% if album %}
        <h2> Hi, {{ user.first_name }}. You have some photos to choose from.
        </h2>
        <div>
            Click <a href="
                {% url 'client:album_details' album.id %}">here</a> </div>

    {% else %}
        <h2>
            Hi, {{ user.first_name }}. You don't have any photos uploaded yet.</h2>
        <div>
            Wanna book me ? Click <a href="{% url 'client:bookme' %}">here</a> </div>
    {% endif %}

urls.py

url(r'^user/(?P<pk>[0-9]+)/$', user_album_details, name='album_details'),

last but not least, the call to the user_album_details functions in views.py is :

@login_required(login_url='login')
def user_album_details(request, pk):
    album = get_object_or_404(Album, id=pk)
    return render(request, 'client/photo_details.html', {'album': album})

The error :

NoReverseMatch at /client/user/

Reverse for 'album_details' with arguments '('',)' not found. 1 pattern(s) tried: ['client/user/(?P<pk>[0-9]+)/$']

Partial solution:
if I replace album.id in index.html as follow :

<div>Click <a href="{% url 'client:album_details' 1 %}">here</a></div>

it works but with album.id or album.pk or even album_id, it doesn’t work

I’ve found something, when I tried to print the id in the html code as this :
<div> Album id is : <a href="#">{{ album.pk }}</a> </div>


So I guess I can’t have the id like this, even though I try album.id, I have the same result.

Your context contains album, which is a queryset, not a single album. (It could be multiple albums)

Your template is trying to get the id attribute of an album, except album isn’t an Album. It’s a queryset referring to (possibly) many Album.

You either need to iterate through album to generate this link for each one, or else select which album to use from the queryset.

Thank you so much, that resolved my problems and all that followed since I know that now. I used album[0].id and made sure to use some logic in case album is empty. Thanks again !!!