I completed Django's 7 part tutorial and am now reading both the official documentation about flat pages as well as this other site.
in
my_project/settings.py
I have added the sites and flatpages apps as well as the SITE_ID.
in
my_project/urls.py
I have added
urlpatterns += [url(r'^pages/', include('django.contrib.flatpages.urls')),]
and because I also need to have flatpages/default.html as a template, in
my_project/templates/flatpages/
there is a file named default.html
to make sure it is found back in
my_projects/settings.py
I updated templates to look like:
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',
],
},
},
]
So I went onto the admin page and added a flatpage named "test" and then went to ...8000:/pages/test/
to get an error message TemplateDoesNotExist.
So where did I go wrong?
In order for your current DIRS in your TEMPLATES setting to work, make sure that your templates/flatpages/ directory is in your outer my_project directory (the one that contains manage.py), not the inner directory (the one that contains settings.py).
Related
I am using Django 2.2.10
I have an app called myapp, and this is my folder structure:
/path/to/project
...
myapp
...
templates
myapp
index.html
When I place index.html in /path/to/project/myapp/templates/index.html I am able to view the template, however, when I place index.html in the correct subfolder (as shown above) and recommended in the Django documentation (as a way of "namespacing"the templates per application).
I get the eror TemplateNotFound.
This is the relevant portion of my settings.py
settings.py
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',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
],
},
},
]
Why is Django not able to find the template, and how do I fix this?
Make sure, that in 'settings.py' your app name ('myapp') is in 'INSTALLED_APPS' and make sure you are calling template by 'myapp/index.html' in your 'views.py'.
I'm using django version 2.1.2 with python 3.6.
I created two django projects (test01 & test02) by CMD.
Both of the projects are under the same folder.
Test01 executes normally, while test02 raises TemplateDoesNotExist error.
I've found a solution for the latter that is hard coding the address of templates in settings.py:
'DIRS': [r'C:\django\test02\accounts\templates']
However, another project can run normally even leaving this list as blank [].
The structures of both projects are the same:
Can anyone give a suggestion that can fix the problem in test02 without hard coding the address of templates in test02?
You may notice a built-in Django variable named BASE_DIR, it represents your root project, so you don't need to hard code the absolute path.
Add this in settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# codes...
],
},
},
]
Register all your apps, and Django will look for any files inside a folder named templates as you mentioned in os.path.join(BASE_DIR, 'templates')
Let create a folder called test01App in template and create base.html on it.
Then you can call test01App/base.html in response.
BACKEND is default of Django and you have to create folder templates.
You can customize where it stores template in other place in DIRS.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, './cuong')],
'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',
],
},
},
]
Alright, so I'm not that new to programming, but I still have newbie problems.
I'm using Python 2.7 , Django 1.8
I installed django-fobi around 2 days ago and have been pulling my hair since.
Things are better now, but one error refuses to resolve:
TemplateDoesNotExist at 127.0.0.1:8000/fobi/forms/create/ the template is: bootstrap3/create_form_entry.html
although the loader does find it:
project_name/fobi/contrib/themes/bootstrap3/templates/bootstrap3/create_form_entry.html (File exists)
However, the other templates of fobi are working.
To be honest, I'm not sure if I configuredfobi correctly (newbie issues).
Here are my files:
project_name/settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [(os.path.join(SETTINGS_PATH, 'templates'))],
# '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',
"fobi.context_processors.theme", # Important!
"fobi.context_processors.dynamic_values", # Optional
"django.core.context_processors.request"
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'admin_tools.template_loaders.Loader',
],
# 'debug': DEBUG_TEMPLATE,
},
},
]
And I copied the fobi folder from https://github.com/barseghyanartur/django-fobi/tree/master/src and put it at project_name/fobi
Initially, when I did not have the full fobi folder copied, I had just a few templates copied to project_name/templates/bootstrap3/create_form_entry.html, and the template loaded, but then the reversed url fobi_theme.create_form_entry didn't work.
All migrations are applied and the apps are added in INSTALLED_APPS.
Please, help and thank you in advance.
I was recently trying to learn Django for one of my private project.
When came to the Chapter on Template,the Django Book recommended setting template path in settings.py using the the following snippet
TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),)
However ,when I opened the file setting.py I found nothing like "TEMPLATE_DIR" but a list:
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',
],
},
},
]
the the value related to the key "DIR",was a empty list. So I try to filled it with the content shown above.
And then, code something in the views.py(all the import was done )
def current_datetime(request):
now = datetime.datetime.now()
t = get_template("current_datetime.html")
html = t.render(Context({"current_date" : now}))
return HttpResponse(html)
And then mkdir templates in the same folder with setting.py , saved current_datetime.html in folder templates
Finally,run the project.and got the message in my terminal:
WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 > and the TEMPLATES dictionary takes precedence. You must put the values of the > following settings into your default TEMPLATES dict: TEMPLATE_DIRS.
System check identified 1 issue (0 silenced).
June 15, 2017 - 15:32:49
Django version 1.11.2, using settings 'mysite.settings'
Starting development server at 127.0.0.1:8000/
Quit the server with CONTROL-C.
When opened the address (127.0.0.1:8000/time/) in my Safari,here came the
error message:
enter image description here
Anyone help,please ??
There is a warning on TEMPLATE_DIRS, for that just be sure you don't have TEMPLATE_DIRS variable in your settings.py and restart development server.
Then for the error, you are actually using a Context object instead of a dict, you should render the template using a useful shortcut https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#example
from django.shortcuts import render
def current_datetime(request):
now = datetime.datetime.now()
return render(request, "current_datetime.html", {
'current_date' : now,
})
I think you have BASE_DIR in settings.py for define location so use like 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',
],
},
},
DIRS should be like below
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
I have the following directory structure in my django 10 project:
/my-project/ # project dir
+app1
+templates
+admin
base.html
404.html
500.html
My templates attribute looks like this in settings:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'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',
'common.context_processors.site_info',
],
},
},
]
My custom base.html displays on my local machine. When I upload this to my production server it no longer overrides and uses the base.html file in project folder.
I have changed around the order of apps suggested here and tried printing the dirs element of the templates attribute which prints "templates/" like here.
Does anyone know how I can get this to work on my production environment?
You must use absolute path in your settings to avoid issues. For example:
import os
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')
# depending where your settings.py live
...
'DIRS': [
os.path.join(PROJECT_ROOT, 'templates'),
],