Reputation: 67
Python 2.7 & Django 1.10 my template exist but i do somesing wrong!
TemplateDoesNotExist at /basicview/2/
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
This is template_two view!
</body>
</html>
Request Method: GET
Request URL: http://127.0.0.1:8000/basicview/2/
Django Version: 1.10.1
Exception Type: TemplateDoesNotExist
Exception Value:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
This is template_two view!
</body>
</html>
Exception Location: /home/i/djangoenv/local/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg/django/template/loader.py in get_template, line 25
Python Executable: /home/i/djangoenv/bin/python
Python Version: 2.7.11
Python Path:
['/home/i/djangoenv/bin/firstapp',
'/home/i/djangoenv/lib/python2.7',
'/home/i/djangoenv/lib/python2.7/plat-i386-linux-gnu',
'/home/i/djangoenv/lib/python2.7/lib-tk',
'/home/i/djangoenv/lib/python2.7/lib-old',
'/home/i/djangoenv/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-i386-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/i/djangoenv/local/lib/python2.7/site-packages',
'/home/i/djangoenv/local/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg',
'/home/i/djangoenv/lib/python2.7/site-packages',
'/home/i/djangoenv/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg']
Server time: Пт, 23 Сен 2016 15:43:30 +0000
settings.py (os.path.join(BASE_DIR), 'templates', or /home/mainapp/templates) not working..
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['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',
],
},
},
]
article/views.py my def looks like:
def template_two(request):
view = "template_two"
t = get_template('myview.html')
html = t.render(Context({'name': view}))
return render(request, html, {})
My file:
mainapp/mainapp/settings.py
mainapp/mainapp/article/views.py
mainapp/templates/myview.html
Upvotes: 1
Views: 7366
Reputation: 1
Make "DIRS" blank in "TEMPLATES" in "settings.py". Django will find "templates" folder automatically:
# "mainapp/settings.py"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 'DIRS': ['templates'],
'DIRS': [], # Here
'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',
],
},
},
]
Upvotes: 0
Reputation: 1
as of 05/05/2021, everything I had to do to resolve this error added the full path to the "DIRS" in settings.py
example:
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [r'the path to the templates folder here'],
'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',
],
},
},
]
Upvotes: 0
Reputation: 41
In Django 3.1.7, I just restarted the server "python manage.py runserver", it worked. My entry in settings.py is
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
Upvotes: 0
Reputation: 749
Step 1: copy path of your Template.
Step 2: past copy path in DIRS in your setting.py >> "Template"
its work for me
Upvotes: 0
Reputation: 308839
The problem is that you are manually rendering your template and using the render
shortcut at the same time. Your get_template
is working, but when you call render(request, html, {})
, Django is treating html
as the filename, and looking for a template file named <!DOCTYPE html>\n<html>...
.
You should either render the template manually:
def template_two(request):
view = "template_two"
t = get_template('myview.html')
html = t.render({'name': view}) # Note you should use a plain dictionary, not `Context` on Django 1.8+
return HttpResponse(html)
Alternatively, it's simpler to use the render
shortcut.
def template_two(request):
view = "template_two"
return render(request, "myview.html", {'name': view})
You should also change your DIRS
setting back to use os.path.join(BASE_DIR, 'templates')
. Using the string 'templates'
is not going to work.
Upvotes: 1
Reputation: 821
I would suggest that you put your temlates in your app.
Your file will then be here:
mainapp/mainapp/templates/myview.html
Please make sure you add mainapp
to your INSTALLED_APPS
like this:
INSTALLED_APPS = [
...
'mainapp',
]
Upvotes: 6
Reputation: 23484
In your settings.py you have 'DIRS': ['templates'],
And path to your template is mainapp/templetes/myview.html
You have typo templetes != templates
. Rename folder with templates to templates
.
Upvotes: 2