Tristan Tran
Tristan Tran

Reputation: 1513

Calling view name that belongs to a different app

I am trying to a similar task as in this thread here : calling a view when a button is clicked. However, the view name that the button is calling belongs to a different app.

<button class="button table_mgt" >
      <a href="{% url 'table_view' %}">Table Mgt</a>
</button>

This button sits in a template called dashboard.html which belongs to an app called account. But the view named table_view belongs to another app called table. Both apps belong to the same root.

What do I need to set up for the above code to work?

Update more details: Here is the error I received:

Request URL:    http://127.0.0.1:8000/table/tables/
Django Version:     3.0.7
Exception Type:     TemplateDoesNotExist
Exception Value:    table_view.html

In my table app: File urls.py

from django.urls import path, include
from . import views

urlpatterns = [
    path('tables/', views.table_view, name='table_view'),
]

File view.py

from django.shortcuts import render
from .models import Table

def table_view(request):
    table_num = Table.objects.count()
    return render(request,
                  'table_view.html',
                  {'table_num': table_num})

The template table_view.html sits in this folder \table\templates\table\

File urls.py of the project root:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('account/', include('account.urls')),
    path('table/', include('table.urls')),
]

In my root project, there is also a folder \templates where I keep the base.html for all apps to use.

Upvotes: 1

Views: 354

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476719

If both apps belong to the same project, you do not need to change anything. The fact that these belong to different apps doesn't matter.

If you however have an app_name in the urls.py of the table_view, you need to prefix it with the namespace. For example if the urls.py where the table_view is defined looks like:

# account/urls.py

app_name = 'account'

urlpatterns = [
    # …,
    path('some-path', table_view, name='table_view')
]

then you prefix it with the app_name in the urls.py:

<button class="button table_mgt" >
      <a href="{% url 'account:table_view' %}">Table Mgt</a>
</button>

The same happens when you defined a namespace=… when you included the urls, for example:

# urls.py

urlpatterns = [
    # …,
    path('', include('account.urls', namespace='account')),
]

For more information, see the URL namespaces section of the documentation.

The template table_view.html sits in this folder \table\templates\table\.

Then the name of the template is table/table_view.html, so:

def table_view(request):
    table_num = Table.objects.count()
    return render(
        request,
        'table/table_view.html',
        {'table_num': table_num}
    )

With the default configuration, Django will see all the templates directories of all apps as "roots", so, the roots are:

app1/templates/
app2/templates/
⋮
appn/templates/

you defined in the templates directory an extra directory tables, so that means that in order to access the table_view.html, the path relative to the root is tables/table_view.html.

Upvotes: 3

Related Questions