How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Django · Python

Fix NoReverseMatch: Reverse for 'product_detail' with keyword arguments '{"slug": ""}' not found. in Django

This error means Django's URL resolver cannot find a URL pattern matching the given view name and arguments. Fix it by verifying the URL pattern name matches the reverse call, ensuring the arguments satisfy the URL regex or path converter, and checking that the app's urls.py is included in the root URL configuration.

Reading the Stack Trace

Traceback (most recent call last): File "/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/app/products/templatetags/product_tags.py", line 12, in product_url return reverse('product_detail', kwargs={'slug': product.slug}) File "/venv/lib/python3.11/site-packages/django/urls/base.py", line 88, in reverse return resolver._reverse_with_prefix(view, prefix, *args, **kwargs) File "/venv/lib/python3.11/site-packages/django/urls/resolvers.py", line 828, in _reverse_with_prefix raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'product_detail' with keyword arguments '{"slug": ""}' not found. 1 pattern(s) tried: ['products/(?P<slug>[-\\w]+)/$']

Here's what each line means:

Common Causes

1. Empty or None argument passed to reverse

The slug field is empty or None, so the generated URL does not match the pattern that requires a non-empty slug.

# Product has an empty slug
product = Product.objects.create(name='Test', slug='')  # Empty slug

# In template:
{% url 'product_detail' slug=product.slug %}  # Fails with empty slug

2. URL name mismatch

The name used in reverse() or {% url %} does not match any URL pattern name.

# urls.py
urlpatterns = [
    path('products/<slug:slug>/', views.product_detail, name='product-detail'),
]

# template.html — wrong name (underscore vs hyphen)
{% url 'product_detail' slug=product.slug %}

3. App namespace not included in reverse

The URL uses an app namespace but the reverse call does not include it.

# root urls.py
urlpatterns = [
    path('products/', include('products.urls', namespace='products')),
]

# template — missing namespace
{% url 'product_detail' slug=product.slug %}
# Should be: {% url 'products:product_detail' slug=product.slug %}

The Fix

Ensure the URL pattern name matches exactly what reverse() or {% url %} uses. Guard against empty slugs in templates, and auto-generate slugs in the model's save method to prevent empty values from reaching the database.

Before (broken)
# urls.py
urlpatterns = [
    path('products/<slug:slug>/', views.product_detail, name='product-detail'),
]

# template.html
<a href="{% url 'product_detail' slug=product.slug %}">{{ product.name }}</a>
After (fixed)
# urls.py
urlpatterns = [
    path('products/<slug:slug>/', views.product_detail, name='product_detail'),
]

# template.html
{% if product.slug %}
  <a href="{% url 'product_detail' slug=product.slug %}">{{ product.name }}</a>
{% endif %}

# models.py — ensure slug is never empty
class Product(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            from django.utils.text import slugify
            self.slug = slugify(self.name)
        super().save(*args, **kwargs)

Testing the Fix

import pytest
from django.test import TestCase
from django.urls import reverse, NoReverseMatch
from products.models import Product


class TestProductURLs(TestCase):
    def setUp(self):
        self.product = Product.objects.create(name='Test Widget', slug='test-widget')

    def test_reverse_product_detail(self):
        url = reverse('product_detail', kwargs={'slug': 'test-widget'})
        assert url == '/products/test-widget/'

    def test_reverse_with_empty_slug_raises(self):
        with pytest.raises(NoReverseMatch):
            reverse('product_detail', kwargs={'slug': ''})

    def test_product_auto_generates_slug(self):
        product = Product.objects.create(name='New Gadget')
        assert product.slug == 'new-gadget'

    def test_product_detail_url_resolves(self):
        response = self.client.get(f'/products/{self.product.slug}/')
        assert response.status_code == 200

Run your tests:

pytest

Pushing Through CI/CD

git checkout -b fix/noreversematch-empty-slug,git add products/models.py products/urls.py templates/,git commit -m "fix: ensure URL name matches and slug is never empty",git push origin fix/noreversematch-empty-slug

Your CI config should look something like this:

name: CI
on:
  pull_request:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'
      - run: pip install -r requirements.txt
      - run: pytest --tb=short -q

The Full Manual Process: 18 Steps

Here's every step you just went through to fix this one bug:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. Monitor production to confirm the error is resolved

Total time: 30-60 minutes. For one bug.

Or Let bugstack Fix It in Under 2 minutes

Every step above? bugstack does it automatically.

Step 1: Install the SDK

pip install bugstack

Step 2: Initialize

import bugstack

bugstack.init(api_key=os.environ["BUGSTACK_API_KEY"])

Step 3: There is no step 3.

bugstack handles everything from here:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. Deploys to production (or opens a PR for review)

Time from error to fix deployed: Under 2 minutes.

Human involvement: zero.

Try bugstack Free →

No credit card. 5-minute setup. Cancel anytime.

Deploying the Fix (Manual Path)

  1. Run the full test suite locally to confirm URL resolution works.
  2. Open a pull request with the URL and model fixes.
  3. Wait for CI checks to pass on the PR.
  4. Have a teammate review and approve the PR.
  5. Merge to main and verify all product links work correctly in staging.

Frequently Asked Questions

BugStack runs the fix through your existing test suite, generates URL resolution tests for all named patterns, and validates that no NoReverseMatch errors occur before marking it safe to deploy.

BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.

Use python manage.py show_urls (from django-extensions) to list all registered URL patterns and their names. In a shell, inspect django.urls.get_resolver().reverse_dict.

Use path() with path converters (slug, int, str) for most cases. Use re_path() only when you need complex regex patterns that path converters cannot express.