'Reverse for 'author_details' with arguments '('',)' not found. 1 pattern(s) tried: ['author_details/(?P<auth_id>[^/]+)/\\Z']

I'm new to python and currently developing a practice app similar to Goodreads just to test and challenge myself.

I have reached the part where I created three pages one with a simple list of books with a link to book details where there is a link to the author profile.

The link between the book title link and the book details page works--but when I click on the author link on the book details page it gives me an error

Reverse for 'author_details' with arguments '('',)' not found. 1 patern(s) tried: ['author_details/(?P<auth_id>[^/]+)/\\Z']

Here is my code.

views.py

from django.shortcuts import render

from .models import Book
from .models import BookAuthor
from .models import BookGenre





def authors_database(request):
    authors_database = BookAuthor.objects.all()
    return render(request,'authors_database.html',{
            'authors_database':authors_database
        })


def book_details(request,book_id):
    book_details = Book.objects.get(pk=book_id)
    return render(request,'book_details.html',{
            'book_details':book_details
        })


def author_details(self,auth_id):
    author_details = BookAuthor.objects.get(pk=auth_id)
    return render(request,'author_details.html',{
            'author_details':author_details

        })




def index(request):
    return render(request,'index.html',{

        })



def discover_books_list(request):
    discover_books_list = Book.objects.all()
    return render(request,'discover_books_list.html',{
            'discover_books_list':discover_books_list
        })


book_details.html

{% extends 'layout.html' %}

    {% block body %}
        <div class="container">
        <div class="row">
            <div class="col-md-4">
                                {% if  book_details.book_cover_image %}
                   <img src="{{book_details.book_cover_image.url}}" height="480" width="300" class="img-responsive">
                {% endif %}<br>
            </div>
            <div class="col-md-8">
            <h3>{{book_details}}</h3>
            <hr>

     
            <strong>By: </strong><a href="{% url 'author_details' auth.id %}">{{book_details.book_author}}</a>
   

            <br>
            <strong>Publication date:</strong> {{book_details.book_publication_date}}<br>
            <strong>In:</strong> 
            {% for book_genre in book_details.book_genre.all %}
                {{book_genre}}<br><br>
            {% endfor %}
            <strong>About: </strong><br><br>{{book_details.book_description}}<br><br><br>


            <table class="table table-hover">
              ...  <thead>

                  <tbody>
                    <tr>
                      <th scope="row">ISBN:</th>
                      <td>{{book_details.book_ISBN}}</td>
                      <td></td>
                      <td></td>
                    </tr>
                    <tr>
                      <th scope="row">Settings:</th>
                      <td></td>
                      <td></td>
                      <td></td>
                    </tr>
                    <tr>
                      <th scope="row">Characters:</th>
                      <td colspan="2"></td>
                      <td></td>
                      <td></td>
                    </tr>
                  </tbody>
            </table>

<br><br>



            <a href="{{book_details.book_amazon_link}}" class="buy_on_amazon_btn">Buy on Amazon</a>
        </div>
        </div>
    </div>
    {% endblock %}

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('',views.index,name='index'),
    path('book_details',views.book_details,name='book_details'),
    path('discover_books_list', views.discover_books_list,name='discover_books_list'),

    path('book_details/<book_id>', views.book_details,name='book_details'),

    path('authors_database',views.authors_database,name='authors_database'),

    path('author_details/<auth_id>/',views.author_details,name='author_details'),


]

is there anything I'm doing wrong? Everything works until the point I make the url in the template.



Solution 1:[1]

The problem is here:

<a href="{% url 'author_details' auth.id %}">

In your view you never pass auth as a context variable, so auth.id will be None, and therefore Django can't find a matching URL.

Without seeing your models (and assuming Book has a foreign key field to BookAuthor called author) you would need to do something like this in your view:

def book_details(request,book_id):
    book_details = Book.objects.get(pk=book_id)
    return render(request,'book_details.html',{
            'book_details':book_details,
            'auth': book_details.author,
    })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iri