Collins Orlando
Collins Orlando

Reputation: 1681

Django: Why are some templates not detected?

Django newbie here. I'm working on a project with a couple of apps and, thus, have kept my templates at my project's level. Now, the issue here is some templates are not being detected at the corresponding urls. For instance, the template (property_list.html) corresponding to properties list is detected just fine at the relevant url (/properties), while neither property_detail.html nor property_new.html corresponding to properties/new and properties/[insert property ID] respectively are. Just for the record, Home, Sign up, Log in work just fine.

I have looked up similar instances, both here as well as at other places, but nothing seems to be pointing me in the direction I want. So, what gives?

A screenshot of the template structure is in the linked image below. Again, the folder is at root/project level.

Template Structure

enter image description here

Project Settings (Template Section)

# ...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# ...

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# ...

Project Urls

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

urlpatterns = [
    path('', HomePageView.as_view(), name='home'),
    path('admin/', admin.site.urls),
    path('accounts/', include('allauth.urls')),
    path('agencies/', include('agencies.urls')),
    path('properties/', include('properties.urls')),
]

Properties Urls

from django.urls import path
from . import views

urlpatterns = [
        path('', views.PropertyListView.as_view(), name='property_list'),
        path('new/', views.PropertyCreateView.as_view(), name='property_new'),
        path('<slug:pk>/', views.PropertyDetailView.as_view(), name='property_detail'),
]

Properties Views

from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy

from .models import Property

# Create your views here.
class PropertyListView(ListView):
        model = Property
        template_name = 'property_list.html'

class PropertyDetailView(DetailView):
        model = Property
        template_name = 'property_detail.html'

class PropertyCreateView(LoginRequiredMixin, CreateView):
        model = Property
        template_name = 'property_new.html'
        fields = [
                'property_type', 
                'is_for_sale', 
                'cost', 
                'location', 
                'num_of_bedrooms', 
                'num_of_bathrooms', 
                'num_of_parking_spaces', 
                'num_of_garages',
                'has_pool',
                'has_waterfront',
                'has_elevator',
                'added_on',
        ]

        login_url = 'login'

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

# class PropertyUpdateView(LoginRequiredMixin, UpdateView):
#       model = Property
#       fields = ['title', 'body', ]
#       template_name = 'property_edit.html'
#       login_url = 'login'

# class PropertyDeleteView(LoginRequiredMixin, DeleteView):
#       model = Property
#       template_name = 'property_delete.html'
#       success_url = reverse_lazy('property_list')
#       login_url = 'login'
#       

Upvotes: 2

Views: 840

Answers (2)

ABC
ABC

Reputation: 199

Django standard is to create a "templates" folder for every app you have.

This templates folder should then contain yet another folder called like the app name.

Upvotes: 0

Ramy M. Mousa
Ramy M. Mousa

Reputation: 5933

Since in your settings you have 'DIRS': [(os.path.join(BASE_DIR, 'templates'))] then by default django looks for file names under the directory named "templates" on your root.

In your templates directory structure you added another directory named properties which includes property_list.html so, for the view to find the template you have to specify the relative path starting template directory.

template_name = 'properties/property_list.html'
template_name = 'properties/property_new.html'

Enjoy.

Upvotes: 1

Related Questions