Path to template directory not working in settings.py - python

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

Related

How to fix this error: Template Does Not Exist?

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',
]
},
},
]

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',
],
},
},
]

Loading html template using django fails

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

Django Global base.html template

I am new to Django. I am using Django 1.8.6 with Python 2.7. I am trying to use a base.html template that can be used globaly through out the entire site, where every app and access it. Here is my test site's current structure:
twms
polls
migrations
static
templates
project
migrations
static
templates
project
index.html
tmws
static
templates
tmws
base.html
Here is the code for project/templates/project/index.html
{% extends 'tmws/base.html' %}
{% block content %}
<h1>Projects</h1>
<ul>
{% for project in project_list %}
<li>{{ project.name }}</li>
{% endfor %}
</ul>
end of list
{% endblock %}
This is the error I am receiving:
TemplateDoesNotExist at /project/
tmws/base.html
How do I access tmws/tmws/templates/tmws/base.html from any of my apps?
Any help would be greatly appreciated!
Let me know if any additional information is needed.
EDIT
Here are my template settings from settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'), # If i leave both or just comment one one out I still get the same error
'tmws.tmws.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',
],
},
},
]
I think you might be having a problem with your template directory configuration. In your project´s settings.py try to check if you have a configuration similar to this one:
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, 'your template dir name')]
,
'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',
],
},
},
]
Where 'your template dir name' should be tmws. This will make django search for this directory whenever you try to extend templates in your HTML. You can add as many directories as you want.
I think right now Django must be searching for the template in:
twms/project/templates/project
So maybe if you place your base.html file there Django will be able to find it.
Another suggestion would be to create a general templates directory and place your base.html there since you want it to be used in the entire site. This is just my taste for ordering templates, but it should work either way.
Edit:
Adding the following in settings.py solved the problem. Read comments for more info:
MASTER_BASE_DIR = os.path.dirname(__file__)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(MASTER_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',
],
},
},
]
Check the TEMPLATES value in your settings. You can add the DIR option with a directory where you can put all your common templates.
Link: https://docs.djangoproject.com/en/1.9/ref/settings/#templates
You need to add all templates folders to the TEMPLATES settings in settings.py. Django will then treat them as if they're all in the same folder. It seems like you have not added all of them. You want your TEMPLATES to look something like this:
TEMPLATES = [
{
'DIRS': [
'twms.tmws.templates', # Since this is not in an app
],
'APP_DIRS': True, # Automatically include the templates from INSTALLED_APPS
},
]
You set up the directories to search for the templates as follows:
'DIRS': [os.path.join(MASTER_BASE_DIR, 'templates'),]
where it should read instead:
'DIRS': [os.path.join(MASTER_BASE_DIR, 'tmws/templates'),]
Why? because BASE_DIR is defined as follows:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# -> os.path.dirname(os.path.dirname(/your/path/tmws/tmws/settings.py))
# -> /your/path/tmws/
So BASE_DIR points to your top folder, if you then join it with 'templates', it becomes: /your/path/tmws/templates, which doesn't exist. However, with the changed line in the DIRS list, it will become /your/path/tmws/tmws/templates which is the correct one.

Categories

Resources