Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : TemplateDoesNotExist at /.../

Tags:

python

django

I'm trying to render a simple page, but I encountered a problem.

TemplateDoesNotExist at /pages/  
{}

The template folder is not found. here is my configurations of settings.py

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, 'pages/template')],
        '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',
            ],
        },
    },
]

Have I done something wrong ?

--------------- CMD ----------------

Internal Server Error: /pages/
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Python34\Scripts\src\pages\views.py", line 6, in index
    return render('index.html', context)
  File "C:\Python34\lib\site-packages\django\shortcuts.py", line 67, in render
    template_name, context, request=request, using=using)
  File "C:\Python34\lib\site-packages\django\template\loader.py", line 96, in render_to_string
    template = get_template(template_name, using=using)
  File "C:\Python34\lib\site-packages\django\template\loader.py", line 43, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: {}
[12/Jan/2016 20:51:57] "GET /pages/ HTTP/1.1" 500 74646

----------- UPDATE -------------

File Tree

enter image description here

views.py

from django.shortcuts import render

# Create your views here.
def index(request):
    context = {}
    return render(request, 'index.html', context)
like image 860
Hiroyuki Nuri Avatar asked Jan 12 '16 20:01

Hiroyuki Nuri


People also ask

How do I fix Django TemplateDoesNotExist error?

Django TemplateDoesNotExist error means simply that the framework can't find the template file. To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file ( settings.py ) by TEMPLATE_DIRS setting.

What template engine does Django use?

The Django template language is Django's own template system. Until Django 1.8 it was the only built-in option available. It's a good template library even though it's fairly opinionated and sports a few idiosyncrasies.

What is Django bootstrap?

Django with Bootstrap. Bootstrap is a framework which is used to create user interface in web applications. It provides css, js and other tools that help to create required interface. In Django, we can use bootstrap to create more user friendly applications.


2 Answers

You probably forgot to add your app in Installed_Apps setting.

INSTALLED_APPS = [

...

'your_app'  

]

like image 70
Nitish Kumar Avatar answered Oct 24 '22 06:10

Nitish Kumar


You have forgotten the first argument request when you call render.

return render(request, 'index.html', context)

You can have multiple template directories, e.g. src/template and pages/template. If you want to have a src/template directory, then you need to include it in your DIRS option`.

    'DIRS': [os.path.join(BASE_DIR, 'templates')],

You don't need pages/templates in the DIRS directory -- templates in that directory will be found by the app loader because you have APP_DIRS set to True, and pages is in your INSTALLED_APPS setting.

like image 38
Alasdair Avatar answered Oct 24 '22 06:10

Alasdair