How to fix this error: Template Does Not Exist? - python

urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.home, name='home')
]
another urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('subokLang.urls')),
path('admin/', admin.site.urls),
]
settings.py - templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'tryingDjango/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',
],
},
},
views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'templates/home.html')
I am stuck here, tried changing many already, still won't work, the 500(Template does not exist), what is the right code?

In your settings.py file:
inside TEMPLATES, just add this:
[os.path.join(BASE_DIR, 'templates')]
Ex:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #Added 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',
]
},
},
]

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 render template does not exist

I'm using linux, python 3.4, django 1.8.2, pytmysql
In my virtualenv there are:
db.sqlite3 manage.py new/ templates/
settings.py:
First I commented 'DIRS':[],
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',
],
},
},
]
then add this part:
TEMPLATE_DIRS = (
'/home/niloofar/django/niloofar/new/templates',
)
urls.py:
from new.views import welcome
urlpatterns = [
url(r'^welcome', welcome),
]
views.py:
from django.shortcuts import render
def welcome(request):
message = "welcome again:D"
return render(request, 'base.html', {'message': message})
In templates directory, there is base.html file:
<html>
<body>
<p>* {{ message }} *</p>
</body>
</html>
When I refresh the page it prints error:
Exception Type: TemplateDoesNotExist
Exception Value: base.html
Try this, don't need to comment DIRS.
The setting TEMPLATE_DIRS is deprecated. see the link https://docs.djangoproject.com/en/1.9/ref/settings/#template-dirs
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__), '..//', 'templates').replace('\\', '/')],
'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',
],
},
},
]

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

Django 1.8 not using Jinja2 template engine

I have followed the instructions on using jinja2 with django1.8. --->
#settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
normpath(join(DJANGO_ROOT, 'templates/jinja2')),
],
'APP_DIRS': True,
'OPTIONS': {
'environment': 'kuyuweb_dj_1_8.jinja2.environment',
},
},
{
'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',
'django.core.context_processors.request',
],
},
},
]
I have a .py file including environment -->
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.urlresolvers import reverse
from jinja2 import Environment
def environment(**options):
env = Environment(**options)
env.globals.update({
'static': staticfiles_storage.url,
'url': reverse,
})
return env
and in my application folder i have templates/jinja2 folder.
I have created a simple view as:
def home(request):
works = Work.objects.filter(publish=True).order_by('-created_at')[:8]
return render(request, 'jinja2/home.html', {'works':works })
But for example when i try to use jinja2 template tag as {{ loop.index }} it does not work. {{ forloop.counter }} is still working.
Is there something that i miss?
The jinja templates for your app should be in yourapp/jinja2, not yourapp/templates/jinja2.
If the template is at yourapp/jinja2/home.html, then your render line should be
return render(request, 'home.html', {'works':works })

Categories

Resources