TemplateDoesNotExist at / Exception Value: index.html django - python

welcome
i make a new project and i put index.html in templates dirctory and when i runserver i got the message TemplateDoesNotExist at / where the mistake i done
there is the views.py
from django.shortcuts import render, redirect
def index(request):
return render(request,'index.html')
there is the urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index,name='index'),
]
and there is the sitting.py
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, 'static')
the templates_dir and static_dir i add it after make project

just add TEMPLATES_DIR inside TEMPLATES in settings.py file.
Example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR], #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',
],
},
},
]
And see if it solves that error

Related

Django: Template does not exist

Please can you help me understand what I have done wrong here, I am sure it's a very simple problem but, I am new to django and especially the latest version...
I believe and as far to my research that I have routed my views to the best of my knowledge, please may you help identify my issue, I am getting an error:
"Page Not Found"
url.py from my app
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
views from my app
from django.shortcuts import render
from requests import request
# Create your views here.
def index(request):
return render(request, 'landing_page/index.html')
url from my project folder
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('/', include('comp.urls')),
path('admin/', admin.site.urls),
]
urls from my project
and then here is my project and template folders, I have attached an image for your ease of helping me:
In settings.py file, just add this:
os.path.join(BASE_DIR, 'templates')
EX:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #Removed landing_page from 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',
],
#Removed libraries from here
},
},
]

(django)TemplateDoesNotExist at /

urls.py
from django.urls import path, include
urlpatterns = [
path('', include('calc.urls')),
path('admin/', admin.site.urls),
]
views.py
from django.shortcuts import render
def home(request):
return render(request, 'tmpl/home.html')
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'tmpl')],
'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',
],
},
},
calc/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home')
]
folder structure:
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
in your settings.py inside TEMPLATES do:
'DIRS': [os.path.join(BASE_DIR, 'top', 'tmpl')]
and in views.py
return render(request, 'home.html')

Trying to set up urls.py to direct to index.html

As the question implies, I am trying to set up my urls.py file to point towards my index.html file.
Here is the structure of my project:
-->mysiteX
---->.idea
---->mysite
------->migrations
__init__
admin.py
apps.py
models.py
test.py
views.py
---->mysiteX
-------->templates
index
---->css
---->fonts
---->js
---->vendors
__init__
settings
urls
wsgi
----->venv
db.sqlite3
manage
This is what my urls.py file looks like
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^index/', admin.site.urls),
]
my views.py file
from __future__ import unicode_literals
def index_file(request):
return render(request, "index.html")
settings.py:
import os, sys
abspath = lambda *p: os.path.abspath(os.path.join(*p))
PROJECT_ROOT = abspath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)
TEMPLATE_DIRS = (
abspath(PROJECT_ROOT, 'templates'),
)
When I run manage.py I get this page
Update after making changes to my urls.py file to this extent:
from django.conf.urls import url
from django.contrib import admin
from gradientboost import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^/$',views.index_file,name='index')
]
This is what I am getting
Second Update
changed my settings.py file according to an answer given
import os, sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'mysiteX/templates')
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_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',
],
},
},
]
However still getting this error
You Need To Change Your Urls.py
Because You Are Changing Admin Url Admin Url Is For Admin Login And For Website Control!
and if you add index/ than you need to type in your brower like that domainname.com/index
So if you Want Your index.html show on website home page try my code:
Try This If You browsing this link 127.0.0.1:8000
from django.conf.urls import url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index_file, name='YOUR NAME')
]
And if you trying this link 127.0.0.1:8000/index Try This:
from django.conf.urls import url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/$', views.index_file, name='YOUR NAME')
]
Also Add This In Your Settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'mysiteX/templates')
And Also Add This In your Settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_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',
],
},
},
]
create a html template index.html and your url should be redirected the view.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', your_view, name = 'index'),
]
You don't reference templates from your urls.py. That is for matching routes up to view functions, and it is the view's job to tell Django to render a template, if appropriate. You already have a simple view that renders index.html, you just need to reference it in your urlconf.
If you want your index.html rendered at the root of your site, then you need to add this line to your urls.py (EDIT: fixed the route):
url(r'^$', index_file)
You will also need from mysite.views import index_file at the top of your urlconf.

Django : Template not found/index.html

I am running a website using Django. There is no problem in login. When i logged in and click on some dashboard, it is showing "page not found"(404).
Views.py:
def index(request):
return(request,'obs_app/index.html')
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_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',
],
'libraries': {
'get_by_index':'obs_app.templatetags.templatefilters',
'get_by_key':'obs_app.templatetags.templatefilters',
'get_dict':'obs_app.templatetags.templatefilters',
'get_items':'obs_app.templatetags.templatefilters',
'multiple':'obs_app.templatetags.templatefilters',
},
},
},
]
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from obs_app import views
urlpatterns = [
url(r'index/',views.index , name = 'index'),
path('admin/', admin.site.urls),
path('', views.user_login, name='user_login'),
path('dashboard',views.obs_index, name='admin_dash'),
path('halls/active',views.obs_halls_active,name='active-halls'),
path('halls/pending',views.obs_halls_pending,name='pending-halls'),
path('febs/userlist', views.febs_user_list, name='febs-users'),
path('bookings/user', views.bookings_user, name='bookings-users'),
path('bookings/owner', views.bookings_owner, name='bookings-owner'),
path('cancellation/user', views.cancelled_user, name='cancelled-user'),
path('cancellation/owner', views.cancelled_owner, name='cancelled-owner'),
path('terms/obs', views.terms_conditions, name='terms-conditions'),
path('terms/febs', views.terms_febs, name='terms-febs'),
path('terms/febs/events', views.terms_febs_events, name='terms-febs-events'),
Error i am getting is :
Page not found (404)
The current path, index.html, didn't match any of these.
You have to render template and also fix the indentation
def index(request):
return render(request,'obs_app/index.html')
And also here you need to provide directory instead of file
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
Change:
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
To:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
{
...
'DIRS': [], # DIRS defines a list of directories where the engine should look for template source files, in search order.
...
},
]
You might want to read: Support for template engines
Related part copied here:
DIRS defines a list of directories where the engine should look for template source files, in search order.

How do I properly structure Templates and include them in the settings in Django 1.8.6?

So, here is my current structure:
django_project/
home_app/
__init__.py
migrations/
templates/
home_app/
base.html
admin.py
models.py
views.py
project_backend/
__init__.py
settings.py
url.py
wsgi.py
Now in home_app/views.py I have this very simple piece of code:
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = "home/base.html"
and in the settings.py I set up my templates like this:
import os
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, 'home_app', 'templates'),
# os.path.join(BASE_DIR, 'app_name', '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',
],
},
},
]
This works fine, and I am quite happy using the app/templates/app/some_temp.html approach, but this only works when I include the OS.path.joins for all the apps and templates, as far as I can see. Is there a way to register this structure in the settings by default and I am just overlooking it?
This is just no very pretty and I feel like it would be bad practice to write that down for every single app.:
os.path.join(BASE_DIR, 'home_app', 'templates')
You have 'APP_DIRS': True, so you shouldn't have to edit DIRS to use templates in the home_app/templates/ directory, as long as home_app is in your installed apps.
If you have a template,
home_app/templates/home_app/base.html
then you should use it with:
template_name = "home_app/base.html"
You currently have,
template_name = "home/base.html"
which isn't consistent.

Categories