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.
Related
I'm following along a project from the Python crash course book. I'm supposed to be deploying the webapp to heroku but I keep receiving a Server Error 505 warning.
The problem:
I have tried to do various things with my setings.py file, but none appear to work.
SETTINGS.PY:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [r'\learning_logs\templates\learning_logs',
r'\users\templates\users',],
'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',
],
},
}, ]
Project setup:
learning_log: 11_env learning_log:
.git
learning_log:
....
learning_logs:
templates:
learning_logs:
*all html files including base.html*
...
If anyone requires more information to answer the question, please let
me know.
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',
],
},
},
]
So previously, I was using django-1.8 version & I am in the process of upgrading to django-1.11. When I load my /admin page, I get
Exception Type: TemplateDoesNotExist
Exception Value: admin/index.html
I have tried multiple options and dont know why django is not loading admin templates for django-1.11.
Few more details:
- I use django-jet for the admin interface
- My TEMPLATES in settings looks like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'custom_dir_1'),
os.path.join(BASE_DIR, 'custom_dir_2'),
os.path.join(BASE_DIR, 'custom_dir_3')
],
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.request',
'django.template.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'context_processors.base_context',
],
}
}
]
Let me know if I can provide any other information to debug better. Any guidance in this issue is super appreciated, Have been at this issue for 3rd consecutive day :-(
You must set APP_DIRS to True, i.e.:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [
...
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).
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'),],