template expectations when two models within same Django model file

15 Views Asked by At

I have two different (related) model classes within a Django tennis ladder app: Ladder and Player. For the corresponding views file, I include both sets of corresponding class-based views within the same file. And I also include all the urls in the same file as well. These look as follows:

from django.urls import path
from .views import HomePageView, PlayerListView, PlayerDetailView, \
                   PlayerEditView, PlayerCreateView, PlayerDeleteView, \
                   LadderListView, LadderDetailView, LadderDeleteView

urlpatterns = [
    path('', HomePageView.as_view(), name='home'),
    path('players/', PlayerListView.as_view(), name='players'),
    path('player/detail/<int:pk>/', PlayerDetailView.as_view(), name='player_detail'),
    path('player/edit/<int:pk>/', PlayerEditView.as_view(), name='player_edit'),
    path('player/create/', PlayerCreateView.as_view(), name='player_create'),
    path('player/delete/<int:pk>/', PlayerDeleteView.as_view(), name='player_delete'),
    path('ladder_history/', LadderListView.as_view(), name = 'ladder_history'),
    path('ladder/detail/<int:pk>/', LadderDetailView.as_view(), name = 'ladder_detail'),
    path('ladder/delete/<int:pk>/', LadderDeleteView.as_view(), name='ladder_delete'),
]

Within the project urls I include the path to this urls file as follows.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
    path('', include('players.urls')),
    path('', include('matches.urls')),
    path('accounts/', include('accounts.urls')),
]

Here's the problem. When I click on a button to pull up the ladder_history, Django seems to expect the template to located at

TemplateDoesNotExist at /ladder_history/

players/ladder_list.html

Request Method:     GET
Request URL:    http://127.0.0.1:8000/ladder_history/
Django Version:     4.2.5
Exception Type:     TemplateDoesNotExist
Exception Value:    players/ladder_list.html

I am totally mystified. I do not have a 'ladder_list.html', but rather 'ladder_history.html', as I specify in the ListView.

# players.view.py
...
class LadderListView(ListView):
    model = Ladder
    template = 'ladder_history.html'
    ordering = ['date']
    context_object_name = 'past_ladders'

All templates are in the same TEMPLATES directory, and none of the other models are having a problem. (The Match class is in a different app, because it required separate logic.)

Where is the Exception Value players/ladder_list.html being generated from? The assumption that it would be called 'ladder_list' and not what I said it would be in LadderListView is strange. Also, the addition of 'players' to the path seems to suggest that I am obliged to have a different app for every model? I was under the impression you could have related models within the same app.

How do I solve this?

0

There are 0 best solutions below