Natalie
Natalie

Reputation: 471

Django-tables2 linkify to other app of project: TemplateDoesNotExist error

Since my project is a bigger one, I have several apps with each app having some database tables (with each table in its own file): project

    --project
    -all 
    -overview
    -home

In all/probe_list.html I have a table which displays Probe objects. I want to link the probe name to a details page that is in the overview app (overview/probe_details.html). The link works and shows the correct URL 127.0.0.1:8000/overview/probe_detail/1 but I get TemplateDoesNotExist error since django searches for all/probe_detail.html.

project/urls.py

urlpatterns = [
    path('', include('home.urls')),
    path('all/', include('all.urls', namespace='all')),
    path('overview/', include('overview.urls', namespace='overview')),
]

all/urls.py

app_name = 'all'
urlpatterns = [
     path('probe_list', views.AllProbesView.as_view()),
]

all/models/probe.py

class Probe(Compound):
     targetName = models.CharField(max_length = 100, verbose_name="Target(s)")
     inVivoActivity = models.CharField(max_length=10, verbose_name="In vivo use");
     mechanismOfAction = models.CharField(max_length = 255, verbose_name="Mechanism")

    def get_absolute_url(self):
          return reverse('overview:probe_detail', args=[str(self.id)]) 

    def __str__(self):
          return f'{self.name}, {self.targetName}, {self.mechanismOfAction},{self.inVivoActivity}'

    class Meta:
        app_label = 'all'

all/tables.py

class AllProbesTable(tables.Table): 
      name = tables.Column(linkify=True)   
      class Meta:
         model = Probe       
         template_name = "django_tables2/bootstrap5-responsive.html"     
         sequence = ("targetName", "name",  "mechanismOfAction" ,"inVivoActivity",)
         exclude = ("id",)

all/views.py

class AllProbesView(SingleTableView):
      model = Probe
      table_class = AllProbesTable
      queryset = Probe.objects.all()
      template_name = "all/probe_list.html"

all/templates/all/probe_list.html

{% load render_table from django_tables2 %}
{% render_table table %}

overview/urls.py

app_name = 'overview'
urlpatterns = [
    path('probe_detail/<int:pk>', views.ProbeDetailView.as_view(), name="probe_detail"),
]

overview/views.py (not ready yet)

class ProbeDetailView(DetailView):
      model=Probe

overview/templates/overview/probe_detail.html (not ready yet)

{% extends 'baseHeaderIcon.html' %}
{% block header %}Probe details{% endblock header %} 

If I click a link I get:

TemplateDoesNotExist at /overview/probe_detail/1
all/probe_detail.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/overview/probe_detail/1
Django Version: 5.0.1
Exception Type: TemplateDoesNotExist
Exception Value: all/probe_detail.html
Raised during:  overview.views.ProbeDetailView
Python Version: 3.12.1

It searches for all/probe_detail.html instead for overview/probe_detail.html. What I am missing?

Upvotes: 0

Views: 50

Answers (1)

Ben
Ben

Reputation: 2557

It's likely looking in all/ because Probe has app_label='all'.

A solution is to explicitly set the template_name in the ProbeDetailView class.

class ProbeDetailView(DetailView):
    model=Probe
    template_name = 'overview/probe_detail.html'

Upvotes: 0

Related Questions