Reverse for ‘listing_detail’ with keyword arguments ‘{‘pk’: ”}’ not found. 1 pattern(s) tried: [‘listings/listings/(?P[0-9]+)/\Z’]

I’m new to django and I’m creating a listing site, similar to Gumtree. When I try to retrieve the logged-in user’s listings from the database and display on their MyListings page, I get the above error. My code is below. Any help would be appreciated!

Also, I have checked, each listing has a pk value.

urls.py

from django.urls import path
from listings import views

urlpatterns = [
    path('my_listings/', views.my_listings, name='my_listings'),
    path('create/', views.create_listing, name='create_listing'),
    path('listings/<int:pk>/', views.listing_detail, name='listing_detail'),
    path('listings/<int:pk>/update/', views.update_listing, name='update_listing'),
    path('listings/<int:pk>/delete/', views.delete_listing, name='delete_listing'),
]

views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Listing
from users.models import Profile
from .forms import ListingForm
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.db import IntegrityError
from django.core.exceptions import ObjectDoesNotExist

User = get_user_model()

@login_required
def my_listings(request):
    print("my_listings view called")
    context = {} #initialise the context dictionary
    if request.user.is_authenticated:
        try:
             # Get logged-in user
            logged_in_user=request.user
            # Get user profile
            profile=get_object_or_404(Profile, email=logged_in_user.email)
            print(f"Profile for user {request.user}: {profile}")
            listings = Listing.objects.filter(profile=profile)
            print(f"Listings for profile {profile}: {listings}")

            for listing in listings:
                print(f"Listing PK: {listing.pk}")

        except ObjectDoesNotExist:
            print(f"Profile does not exist for user {request.user}")
            # If the profile doesn't exist, handle the error
            profile = None
            listings = Listing.objects.none()

        context['listings'] = listings
        print("Context listings:", listings)
        
        return render(request, "my_listings.html", context)
    else:
        # If user not logged-in, redirect to login page
        return redirect('login')

my_listings.html

{% extends 'base.html' %}
{% url 'create_listing' as create_listing_url %}
{% url 'listing_detail' as listing_detail_url %}
{% url 'update_listing' as update_listing_url %}
{% url 'delete_listing' as delete_listing_url %}

{% load static %}

{% block content %}

<div class="dj-page">
    <div class="container">
        <div class="row">
            <div class="col-md-8 mt-3 offset-md-2">
                <h1>MyListings</h1>
                {% if listings %}
                    <ul>
                        {% for listing in listings %}
                            <div class="listing">
                                <li>{{ listing.title }}</li>
                                <p>PK: {{ listing.pk }}</p>
                                <!-- Debug print statement to check listing PK -->
                                {% if listing.pk %}
                                    <p>Listing PK: {{ listing.pk }}</p>
                                {% else %}
                                    <p>Listing PK is empty or None</p>
                                {% endif %}
                            </div>
                        {% endfor %}
                    </ul>
                    <p>
                        <a href="{% url 'listing_detail' pk=listing.pk %}">View Details</a>
                        <a href="{% url 'update_listing' pk=listing.pk %}">Update Listing</a>
                        <a href="{% url 'delete_listing' pk=listing.pk %}">Delete Listing</a>
                    </p>
                {% else %}
                    <p>No listings yet - <a href="{% url 'create_listing' %}">create your first listing here</a>!</p>
                {% endif %}

                {% if listings %}
                    <p><a href="{% url 'create_listing' %}">Create Listing</a></p>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật