Loading html template using django fails - python

The following is my folder structure
geoff
settings.py
urls.py //using this
wsgi.py
homepage
migrations->folder
_init_.py
.....others here
views.py
index.html
In my geoff urls //root url
from homepage import views as homepage
urlpatterns = [
url(r'^$', homepage.home, name = 'homepage')
]
NOw the homepage views.py
def home(request):
template = loader.get_template('index.html')
context = RequestContext(request, {
'latest_poll_list': "new",
})
return HttpResponse(template.render(context))
The above throws an error
TemplateDoesNotExist at
homepage.html
What could be wrong?
UPDATE ON SETTINGS.PY
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'homepage.apps.HomepageConfig'
)
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',
],
},
},
]
As from the above ive already included Homepage app in settings but still fails to work.

Your TEMPLATES settings should be like this:
TEMPLATES = [
{
####
'DIRS': [os.path.join(BASE_DIR, 'templates')]
####
}
]
then your folder structure:
templates
index.html
...other html
geoff
settings.py
urls.py //using this
wsgi.py
homepage
migrations->folder
_init_.py
.....others here
views.py

Related

TemplateDoesNotExist at / template path

I want to create the backend of a portfolio and I'm trying to use a template but it says it doesn't exist but shows the correct file path in the error message.
This is the urls.py:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('core.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
This is my views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
def home(request):
template = loader.get_template('home.html')
return HttpResponse(template.render(request))
And this is my templates setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
....
],
},
},
]
The directory structure is:
Core is the app name
Give this a try
change the home view to
def home(request):
return render("home.html")
and sett DIRS to blank in settings.py
TEMPLATES = [
{
...
'DIRS': [],
...
}
]
i would suggest you try this in your settings.py
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR = BASE_DIR / 'templates'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'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',
],
},
},
]
and after that request you html in your views.py
def home(request):
return render(request, 'home.html')
and in you urls.py
path('',views.home, name='home')
this better and much cleaner way to tell django where to look fot templates and tell me if you still geting error
I fixed it by moving the statics and template directories into the app directory.

return render method does not working in django

Hi I am very new to the Django and trying to learn going through the vidoes&docs but stuck return the html page from templates.
Though posts/index.html exists, getting the error "TemplateDoesNotExist at /posts/"
not sure where did I wrong, please advise.
here are my directory structure
djangoproject/urls.py
from django.contrib import admin
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/', include('posts.urls')),
]
and Django app called "posts"
posts/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
posts/views
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'posts/index.html')
posts/templates/posts/index.html
<h3>message from index.html</h3>
here is my TEMPLATES from settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'posts/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',
],
},
},
]

Django looks for template under djstripe/templates/djstripe/ instead of djstripe/templates/ (TemplateDoesNotExist)

I'm working on a Django 1.9.7 project using the dj-stripe library. I'm using the latest commit on the #162-api-updates-through_2015-07-28 branch of the dj-stripe repository, which is 1f0ed7c1.
When trying to load a template accounts.html (at /Users/james/venv/lib/python3.5/site-packages/djstripe/templates/djstripe/account.html), I get a TemplateDoesNotExist exception. This accounts.html template extends another template, djstripe/base.html, located at /Users/james/venv/lib/python3.5/site-packages/djstripe/templates/base.html.
Django tries to look for this base.html template at the incorrect path /Users/james/venv/lib/python3.5/site-packages/djstripe/templates/djstripe/base.html.
I tried changing the {% extends "djstripe/base.html" %} to {% extends "base.html" %} in the accounts.html file, but that didn't help.
So Django looks under djstripe/templates/djstripe/ rather than just under djstripe/templates/, resulting in this error.
The message on the debug error page was:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/django/contrib/admin/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/django/contrib/auth/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/djstripe/templates/base.html (Source does not exist)
...
This is my INSTALLED_APPS setting
INSTALLED_APPS = [
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djstripe'
]
This is my TEMPLATES setting
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [],
'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',
'django.template.context_processors.request',
],
},
},
]
This is my urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^', include('djstripe.urls', namespace='djstripe')),
url(r'^admin/', admin.site.urls),
]
In settings file, when 'DIRS' is left empty, Django will look for templates only in app_name/templates directory, because you have set 'APP_DIRS': True, by default.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [],
'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',
'django.template.context_processors.request',
],
},
},
]
In your case, Django is looking for templates in:
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/django/contrib/admin/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/django/contrib/auth/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/djstripe/templates/base.html (Source does not exist)
but there is no base.html in djstripe/templates.
You can move your template files in:
/Users/james/venv/lib/python3.5/site-packages/djstripe/templates/
or you can add path to your templates in DIRS.
here is explanation:
Django DIRS

TemplateDoesNotExist - Creating a simple view

I have the following tree structure:
project/
__init__.py
app/
__init__.py
templates/
home.html
base.html
settings.py
urls.py
views.py
wsgi.py
views.py
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.template.loader import get_template
def home(request):
context = RequestContext(request,
{'user': request.user})
return render_to_response('templates/home.html',
context_instance=context)
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', 'app.views.home', name='home'),
]
relevant parts of settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'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',
],
},
},
]
Problem: When I run:
./manage runserver
Navigate to 127.0.0.1:8000
It raises a django.template.exceptions.TemplateDoesNotExist: templates/home.html
I've tried different variations of the path as well as hardcoding the directory into the TEMPLATES dictionary in my settings file. What am I missing?
This setup was fine. The problem was I was extending base.html in home.html
Like: {% extends 'app/base.html' %} when it needed to be {% extends 'base.html' %}

Path to template directory not working in settings.py

Currently created a templates folder inside my project folder.
Then I added the admin folder and the file base_site.html to be able to change the Django admin title:
Home / Django / mysite / templates / admin / base_site.html
However, it doesn't change. My settings.py file below:
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',
],
},
},
]
Of Avinash Raj's request:
urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
views.py:
from django.shortcuts import render
Since Django 1.7 you do not need to rewrite any templates to change admin title or header but just set site_header, site_title, and index_title in admin.py & then hook them in urls.py. Look here: https://stackoverflow.com/a/24983231/5253807

Categories

Resources