I set an address for the DIRS in TEMPLATES setting in my project's settings.py file and also i change APP_DIRS to False.
now, all of the templates will running but when i go to localhost:8000/admin, it generates an error that said "Template Does Note Exists".
TemplateDoesNotExist at /admin/
admin/index.html
i dont want to APP_DIRS be True.
how can i solve this problem?
this is the settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': False,
'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',
],
},
},
]
If you dont want APPS_DIR to be TRUE, copy templates of admin app to your template directory. When you set APP_DIR to True django will check for a directory named templates in each apps and if found load specified templates from that directory. Else django will check for a directory with same name as of app in the global template directory if found render specified template.
In you case APP_DIR is set to False and there is no directory named as admin in global template folder. So copy template directory from django/contrib/admin/ of your virtual environment and place it in your global template directory. I suppose your global template directory is in the project directory itself.
os.path.join(BASE_DIR, 'templates'),
HTH :)
Related
I'm using django-invitations to manage user invitation.
I can't find where I should put the template files email_invite_message.txt and email_invite_subject.txt
Here they talk about
Override email_invite_message.txt in django-invitations/invitations/templates/invitations/email/.
You can do this by creating the file in the same directory path in your project.
or
Yeah, if this isn't clear, you can create an .html file in your project at {projectroot}/{app}/templates/invitations/email/email_invite_message.html and it will override the default template.
But the first one didn't work for me and I can't figure out what {app} should be.
You can override any third-party application templates by creating replacement files in your project’s templates directory as indicated in the docs here.
In your settings.py file edit the TEMPLATES var, specifically the 'DIRS' key
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/core/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',
],
},
},
]
let's suppose you have an app named core, edit the settings as in the previous example and put the new files in this way (for this particular app):
core/
/templates
/invitations
/email
email_invite_message.html
email_invite_subject.html
__init__.py
admin.py
apps.py
models.py
tests.py
How to define a directory for storing all templates of my app? I am having a hard time finding the explanation how to set templates directory in Django settings.py file.
To set your path to templates folder you need to set your DIRS key with the value in which you are giving your path to templates as shown in the example below where at first i am setting a variable TEMPLATES_DIR where BASE_DIR give me the path to the folder where my manage.py is being kept and i concatinate projects directory alongwith templates directory because in my case i kept it inside project directory.
For example :-
TEMPLATES_DIR = os.path.join(BASE_DIR, 'project','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',
],
},
},
]
So, Here's the explaination What i did is set the TEMPLATES_DIR variable with reference to base directory that will take the path to folder where manage.py of your django project is being kept now i am concatinating the projects and templates directory just to navigate the exact folder structure that you kept for your templates in your project.
I hope you found this helpful. if you have any more doubts i'll be happy to answer those also.
Happy Coding. :-)
The way to set your templates directory in Django is by setting the value of the 'DIRS' key in TEMPLATES inside the settings.py like so (assuming it is present just inside your base directory (BASE_DIR):
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',
],
},
},
]
In Django 3, the templates directory can be added to 'DIRS' like this as well:
'DIRS': [str(BASE_DIR.joinpath('templates'))]
What is the correct way to prepend an extra, site-specific templates directory to the template dirs setting in Django?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
<PREPEND SITE-BASED TEMPLATE DIR IN HERE>
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'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',
],
},
},
]
I obviously can't use Site.objects.get_current() in there.
I could write my own template loader which uses it but is that "correct"?
Or should I be using an extended version of the settings files or something?
First of all, start by separating the settings. You should have a settings hierarchy that goes like:
base_settings.py
siteA_settings.py
siteB_settings.py
On base_settings.py you should put all the settings that are project-wide.
siteA_settings.py (which is a child) will override the base_settings.
To make it site-specific you have to put on each setting file the SITE_ID setting. Example of siteA_settings.py
# Sites Framework
SITE_ID = 1
SITE_TITLE = 'First Site'
Then load the TEMPLATES settings. They can be different on each setting file. Example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
... , etc.
Just like TEMPLATES, you can choose to have different settings for each Site (apps, email settings etc)
Then, when you run the server, you should specify which setting (site) you want to run. Example:
python manage.py runserver --settings=myproject.siteA_settings
where myproject is the app where your settings are located.
Do not forget that you have to put the --settings argument everytime you run a command (including makemigrations, migrate, etc etc)
Usually, on my projects, I create a core app, a settings directory and inside it, I put base_settings.py, dev.py, local.py, production.py etc. When running the Django Development Server, I then run the command:
python manage.py runserver --settings=core.settings.local
I'm going through the very basic tutorial here and I am using my own base_site.html template instead of the default one, which has a different header text.
I copied the default template into my template folder, modified it, then linked the new template in settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
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',
],
},
},
]
No dice.
When I went a much simpler route and instead simply linked it with the single line below, it worked easily:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates/')]
Which is great! But now I want to know why the original code doesn't work.
If it helps, my folder structure is:
- Scripts
- mysite
- mysite
- polls
- templates
- admin
base_site.html
The TEMPLATES setting will be introduced in the django 1.8. It is unavailable in the current django 1.7.
So read the tutorial for the actual version of django you using.
I'm attempting to get the Django development server to load a template with the following settings but it's throwing the error
TemplateDoesNotExist at /
homepage/index.html
Below are the files that I've edited to try to get this to work
blog/blog/settings.py - (Cut the portion pertaining to this question)
import os
TEMPLATE_DIRS = (
os.path.join( os.path.dirname(__file__), 'templates' ),
)
blog/apps/homepage/views.py
from django.shortcuts import render_to_response
def index(request):
return render_to_response('homepage/index.html')
Any thoughts about how to fix this?
By default Django uses two template loaders to locate templates:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
First the filesystem loader checks all dirs in TEMPLATE_DIRS to see if it can find the named template. If that fails the app_directories loader will look in the /templates/ dir in the current app for the named template. This way you can ship default templates with reusable apps that can easily be overridden.
In your case Django will look for the template here:
blog/blog/templates/homepage/index.html
blog/apps/homepage/templates/homepage/index.html
You give a wrong direction to your TEMPLATE_DIRS. The settings.py file is in package blog in project blog, but your templates folder is in blog project, not in blog package. You must give the absolute directory of your project and join templates with it.
in django 1.10, navigate to path/to/settings.py in your parent project directory, look for:
TEMPLATES = [.....] and specify the path/to/project_directory in the DIR: option
for example (in my own case):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/xampp/htdocs/advisory_portal'
],
'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',
],
},
},
]