rgoodwin
rgoodwin

Reputation: 45

NoReverseMatch at /basketball/ Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried:

I have a Django app where a user can create a Basketball object using the Basketball model and post it to a feed that is a ListView subclass. I then want other users to be able to access the details of that post by clicking the hyperlink for the object.

The problem is, when I set the url for the details page to contain the variable "slug" as the object slug, it overwrites my list page with the url route "" as seen in basketball/urls.py, instead of routing to the list page it says it can't find a page with an empty slug. This does not allow me to get to the url "http://127.0.0.1:8000/basketball/"

I'm working under the impression Django searched for url routes in order, so since the blank path is the first route it should be seen before the route containing the slug.

Note the piece of code that is directly causing the issue is in basketball_list.html:

<a href= "{% url 'basketball:detail' basketball.slug %}">

when this code is removed I am able to access "http://127.0.0.1:8000/basketball/" but the hyperlinks aren't there so it defeats the purpose.

Note that I am using: Python == 3.8.3 Django == 3.0.6

models.py:

from django.db import models

from django.db import models
from django.urls import reverse
from django.conf import settings
from django.utils.text import slugify

from autoslug import AutoSlugField
from model_utils.models import TimeStampedModel

from nba_api.stats.static import players

PLAYER_CHOICES = [(i['full_name'], i['full_name']) for i in players.get_active_players()]

class Basketball(TimeStampedModel):
    player = models.CharField(max_length = 50, choices = PLAYER_CHOICES, default = None)
    slug = AutoSlugField("Basketball Player", unique = True, populate_from ="player")
    creator = models.ForeignKey(settings.AUTH_USER_MODEL,
    null = True,
    on_delete = models.SET_NULL
    )

    def get_absolute_url(self):
        return reverse(
        'basketball:detail', kwargs={'slug': self.slug}
        )

    def __str__(self):
        return f"{self.creator} posted about: {self.player}"

views.py:

from django.shortcuts import render
from django.views.generic import ListView, CreateView, DetailView
from django.contrib.auth.mixins import LoginRequiredMixin

from .models import Basketball

class BasketballListView(LoginRequiredMixin, ListView):
    model = Basketball


class BasketballDetailView(DetailView):
    model = Basketball

class BasketballCreateView(LoginRequiredMixin, CreateView):
    model = Basketball
    fields = [
    'player',
    ]

    def form_valid(self, form):
        form.instance.creator =self.request.user
        return super().form_valid(form)

urls.py:

# wtg/basketball/urls.py
from django.urls import path
from . import views


app_name = "basketball"
urlpatterns = [
    path(
    route='',
    view=views.BasketballListView.as_view(),
    name='list',
    ),
    path(
    route='add/',
    view=views.BasketballCreateView.as_view(),
    name='add',
    ),
    path(
    route='<slug:slug>/',
    view=views.BasketballDetailView.as_view(),
    name='detail'
    ),
]

basketball_list.html:

{% extends 'base.html' %}

{% block title %}Basketball List{% endblock title %}

{% block content %}
  <h2>Basketball List</h2>

<ul>
  {% for x in basketball_list %}
    <li><a href= "{% url 'basketball:detail' basketball.slug %}">{{ x }}</a></li>

  {% endfor %}
</ul>

<hr/>
<p>Post Own Info?</p>
  <p>
    <a class="btn btn-primary"
      href="{% url 'basketball:add' %}" role="button">
      Add Post
    </a>
</p>
{% endblock content %}

base urls.py:

urlpatterns = [
    path(
        "",
        TemplateView.as_view(template_name="pages/home.html"),
        name="home",
    ),
    path(
        "about/",
        TemplateView.as_view(template_name="pages/about.html"),
        name="about",
    ),
    # Django Admin, use {% url 'admin:index' %}
    path(settings.ADMIN_URL, admin.site.urls),
    # User management
    path(
        "users/",
        include("wtg.users.urls", namespace="users"),
    ),
    path("accounts/", include("allauth.urls")),
    # Your stuff: custom urls includes go here
    path(
    'basketball/',
    include('wtg.basketball.urls', namespace='basketball')
    ),
    path(
    "^activity/",
    include("actstream.urls")
    ),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 0

Views: 349

Answers (1)

Borut
Borut

Reputation: 3364

Your url doesn't match basketball_list loop variable (x). This

{% for x in basketball_list %}
  <li><a href= "{% url 'basketball:detail' basketball.slug %}">{{ x }}</a></li>
{% endfor %}

should be

{% for x in basketball_list %}
  <li><a href= "{% url 'basketball:detail' x.slug %}">{{ x }}</a></li>
{% endfor %}

Upvotes: 1

Related Questions