I'm new to Django, currently following the tutorial on the Django site for polls:
https://docs.djangoproject.com/en/4.1/intro/tutorial03/
I'm not able to render my template when using polls/index.html. I have followed along verbatim, yet when i try to render the request in the views.py file, I am getting a TemplateDoesNotExist error, which states that the source is not found. What I don't understand is my index.html is located in the correct location (IE, mysite > polls > template > polls > index.html) and my main settings are exactly how the tutorial states. I've tried the frequently suggested (if dated) response of adding os.path.join(SETTINGS_PATH, 'templates'), however the issue persists.
I'm currently running Python 3.10 with Django 4.1, yet this shouldn't be the problem. I've confirmed that my index.html file is in the correct location, being stored in templates > polls > index.html.
settings.py
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',
],
},
},
]
views.py
import requests
from .models import Question
from django.http import HttpResponse
from django.template.loader import get_template
# Create your views here.
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
Error
polls/index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 4.1
Exception Type: TemplateDoesNotExist
Exception Value:
polls/index.html
Exception Location: C:\Users\patbc\myproject\lib\site-packages\django\template\loader.py, line 19, in get_template
Raised during: polls.views.index
Python Executable: C:\Users\patbc\myproject\Scripts\python.exe
Python Version: 3.10.4
Python Path:
['C:\\Users\\patbc\\myproject\\mytestsite',
'C:\\Python310\\python310.zip',
'C:\\Python310\\DLLs',
'C:\\Python310\\lib',
'C:\\Python310',
'C:\\Users\\patbc\\myproject',
'C:\\Users\\patbc\\myproject\\lib\\site-packages']
Server time: Tue, 23 Aug 2022 20:33:42 +0000
Here is my directory structure (note that index.html resides in the proper subdirectory, 'polls'):
link
You have named the folder wrong. It should be plural - templates, not template.
Related
When i put a return render code in my views.py I get an error when i go to http://localhost:8000/
Also why are my paths going through anaconda?
I don't get this error when I do this
from django.http import HttpResponse
def home(request):
return HttpResponse("Yorleì's")
I've included my Template settings
from django.shortcuts import render
# Create your views here.
def home(request):
return render (request, 'homepage/home.html')
TemplateDoesNotExist at /
homepage/home.html
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 3.1.2
Exception Type: TemplateDoesNotExist
Exception Value: homepage/home.html
Exception Location: /Users/yorleydis/opt/anaconda3/lib/python3.8/site-packages/django/template/loader.py, line 19, in get_template
Python Executable: /Users/yorleydis/opt/anaconda3/bin/python
Python Version: 3.8.3
Python Path:
['/Users/yorleydis/yorleico',
'/Users/yorleydis/opt/anaconda3/lib/python38.zip',
'/Users/yorleydis/opt/anaconda3/lib/python3.8',
'/Users/yorleydis/opt/anaconda3/lib/python3.8/lib-dynload',
'/Users/yorleydis/opt/anaconda3/lib/python3.8/site-packages',
'/Users/yorleydis/opt/anaconda3/lib/python3.8/site-packages/aeosa']
Server time: Sun, 29 Nov 2020 12:53:02 -0500
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',
],
},
},
]
I am using python 3.7.2 and Django 2.1 and every time I try to load the home url I get the following error.
TemplateDoesNotExist at /
ghostwriters/post_list.html
Request Method: GET Request URL: http://localhost:8080/ Django
Version: 2.1 Exception Type: TemplateDoesNotExist Exception Value:
ghostwriters/post_list.html
Exception Location:
C:\Users\User.virtualenvs\ghostwriter-HT06mH6q\lib\site-packages\django\template\loader.py
in select_template, line 47 Python Executable:
C:\Users\User.virtualenvs\ghostwriter-HT06mH6q\Scripts\python.exe
Doesn't make any sense because there really is no post_list.html and its not in my app level urls.py or my views.py so why is this happening?
urls.py:
from django.urls import path from .views import PostListView
urlpatterns = [
path('', PostListView.as_view(), name='home'), ]
views.py:
from django.shortcuts import render from django.views.generic import
ListView
from .models import Post
class PostListView(ListView):
model = Post
template = 'home.html'
settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
If you are using any CBV(Class Based Views), by default django will look for template with some specific pattern. In your case, since you are using List View, it will look for YOURMODELNAME_list.html (YOURMODELNAME in lowercase), If you are extending Detail View, it will look for YOURMODELNAME_detail.html .if you want to override this behavior, within your CBV try this,
class YourCBV(ListView):
template_name = 'your_new_template.html'
For your reference,
Django official documentation
Using Django version 1.9.5 and Python 3.
Upon navigation to the url, I am getting the following error:
ImportError at /music/
No module named 'django.templates'
Request Method: GET
Request URL: http://localhost:8000/music/
Django Version: 1.9.5
Exception Type: ImportError
Exception Value:
No module named 'django.templates'
Exception Location: D:\Program Files (x86)\Python\lib\importlib\__init__.py in import_module, line 126
Python Executable: D:\Program Files (x86)\Python\python.exe
Python Version: 3.5.0
Python Path:
['C:\\Users\\ang\\Desktop\\website',
'D:\\Program Files (x86)\\Python\\lib\\site-packages\\django-1.9.5-py3.5.egg',
'D:\\Program Files (x86)\\Python\\python35.zip',
'D:\\Program Files (x86)\\Python\\DLLs',
'D:\\Program Files (x86)\\Python\\lib',
'D:\\Program Files (x86)\\Python',
'D:\\Program Files (x86)\\Python\\lib\\site-packages']
The error seems to be coming from the import line. Checked syntax and tried to explicitly provided the path under TEMPLATES at DIRS but same result. Anyone encountered a similar issue? Found some similar issues raised but in different languages.
Folder Structure for template: name_of_app/templates/inner_folder/html_file
music/templates/music/index.html
views.py
from django.http import HttpResponse
from django.template import loader # error gone if i remove this line
from .models import Album
def index(request):
all_albums = Album.objects.all()
template = loader.get_template('music/index.html')
context = {
'all_albums': all_albums
}
return HttpResponse('test test')
settings.py
TEMPLATES = [
{
'BACKEND': 'django.templates.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.templates.context_processors.debug',
'django.templates.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
This looks like the programmer's greatest nemesis: a typo. You can see midway in the 2nd code snippet a reference to django.templates..., but your 2nd line is importing from django.template.
Edit: From my testing, an incorrect import will fail in the shell, and an incorrect reference in context_processors will fail in the browser.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Try to use this method to render your template using module django.shortcurt.render():
from django.shortcuts import render
from .models import Album
def index(request):
all_albums = Album.objects.all()
context = {
'all_albums': all_albums
}
return render(request, 'music/index.html',context=context)
I am trying to integrate swagger 2.0 with existing DRF application but it gives following error in browser after runningpython manage.py runserver:
TemplateDoesNotExist at /swagger rest_framework_swagger/index.html
Request Method: GET Request URL: http://127.0.0.1:8000/swagger
Django Version: 1.10 Exception Type: TemplateDoesNotExist Exception
Value: rest_framework_swagger/index.html Exception
Location: C:\Users\MHAZIQ~1\Desktop\Tkxel\mmg-git\venv\lib\site-packages\django\template\loader.py
in get_template, line 25
I have added following lines in
views.py:
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='Pastebin API')
And I have added following lines in
urls.py:
url(r'^swagger', views.schema_view),
I ve tried applying following solution:
TemplateDoesNotExist at /docs/ rest_framework_swagger/index.html
but it didnot solve my problem, Can anyone please help me in this regard?
Add 'rest_framework_swagger' to INSTALLED_APPS in Django settings.
settings.py
INSTALLED_APPS = [
...
'rest_framework_swagger',
]
https://django-rest-swagger.readthedocs.io/en/latest/
After several hours of research I have found the problem with my code, as I was adding swagger into an existing project, it didnt have following parameters in settings.py:
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',
],
},
},]
Now swagger works absolutely fine!
I got an error:
TemplateDoesNotExist at /accounts/login/ registration/login.html.
I think I should create login.html file but probably it is not required in Django for default beahivor.
After I placed login.html in accounts/templates/accounts the error has not disappeared.What should I do next?
I wrote in urls.py of accounts,
from django.conf.urls import url
from django.contrib.auth.views import login, logout
urlpatterns = [
url(r'^login/$', login,
name='login'),
url(r'^logout/$', logout, name='logout')
]
in urls.py of parent app,
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
url(r'^api/', include('UserToken.urls')),
]
in TEMPLATES of settings.py of parent app
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',
],
},
},
]
I found in blowser,
emplate-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/templates/registration/login.html (Source does not exist)
I think maybe I should make templates folder.
Now,I made a directory like accounts/registration/accounts/login.html .
I cannot understand why error shows 2 ways directory to admin&auth.Should I make admin&auth directory?
For me worked adding:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Take care to delete browser cache.
The pathnames in your error messages are showing a space character just after login/ - this is probably not contained in the actual filenames, thus the files can't be found. In one case it's a normal space, in the other it's a Unicode 'IDEOGRAPHIC SPACE' (U+3000). I don't see where this is coming from in your source code.
check all your folders name what you put same Error I also receiving.
I mistakenly put template instead of templates.
Django default takes templates folder rather than just template or any other name.
This resolves my problem.