Home > OS >  templates inside application in django
templates inside application in django

Time:04-08

I am working on a django project. I am getting TemplateDoesNotExist error in my browser window. I think django unable to find templates inside application in django project. I am unable to figure it out what is wrong in my code.

project structure is something like that -

schoolproject
    |
    |______ course
    |                 
    |______ fees 
    |
    |______ schoolproject
    |
    |______ manage.py

course application folder -

course
   |
   |___ templates
   |       |
   |       |____ allcourse.html
   |       |
   |       |____ course.html
   |        
   |___ admin.py
   |
   |___ apps.py
   |
   |___ models.py
   |
   |___ tests.py
   |
   |___ urls.py
   |
   |___ views.py

schoolproject/course/views.py

def course(request):
    return render(request, 'allcourse.html')

def course_python(request):
    return render(request, 'course.html')

schoolproject/course/urls.py

from . import views

urlpatterns = [
    path('', views.course),
    path('course-python/', views.course_python)
]

schoolproject/schoolproject/urls.py

import course
import fees

urlpatterns = [
    path('admin/', admin.site.urls),
    path('course/', include('course.urls')),
    path('fees/', include('fees.urls')),
]

On hitting :- 127.0.0.1:8000/course/

enter image description here

And on hitting:- 127.0.0.1:8000/course/course-python/

enter image description here

In settings.py -

TEMPLATES = [
    {   ...
        'DIRS': [],
        'APP_DIRS': True,
        ...  
    },
]

CodePudding user response:

Update your folders as

schoolproject
    |
    |______ course
                  |______ templates
# In templates create a new folder called course
                                   |______ course
    |                 
    |______ fees 
    |
    |______ schoolproject
    |
    |______ manage.py
  • Do this for all apps

In schoolproject/course/views.py

def course(request):
    return render(request, 'course/allcourse.html')

def course_python(request):
    return render(request, 'course/course.html')

In settings.py

TEMPLATES = [
    {   ...
        'DIRS': [BASE_DIR / "templates"],
        'APP_DIRS': True,
        ...  
    },
]
  • Related