I am trying to incorporate a django application into a web site where static html accounts for the majority.
The directory structure is as follows.
root/
├ var/
│ └ www/
│ ├ html/
│ ├ static
│ │ ├style.css
│ │ ├base.js
│ │
│ ├ web/
│ ├head.html
│ ├footer.html
│ ├base.html
│
└ opt/
└ django/
├ project/
│
├ apps/
├ ├ views.py
├ template/
├ index.html
I want to make /opt/django/template/index.html read html in /var/www/html/web/.
I do not know how to include.
{% include "/var/www/html/web/head.html" %}was useless.
I do not want to change the directory structure.
Considering this as your directory structure:
root/
├ var/
│ └ www/
│ ├ html/
│ ├ static
│ │ ├style.css
│ │ ├base.js
│ │
│ ├ web/
│ ├head.html
│ ├footer.html
│ ├base.html
│
└ opt/
└ django/
├ project/
│
├ apps/
├ ├ views.py
├ template/
├ index.html
To use /var/www/html/web/head.html in your index.html.
Go to your settings.py and add this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'apps/template'),'/var/www/html/web/']
,
'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',
],
},
},
]
Now go to your index.html.
{% include "head.html" %}
I hope this will help.
Add /var/www/html/web/ to the DIRS option in the template configuration dictionary in your project settings.
https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-TEMPLATES-DIRS
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/var/www/html/web/'], # won't work on Windows
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
},
},
]
Then:
{% include "head.html" %}
Your templates can go anywhere you want. Your template directories are by using the DIRS option in the TEMPLATES setting in your settings file.
For each app in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django looks for templates in there.
note
Paths should use Unix-style forward slashes, even on Windows.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/var/www/html/web/',
],
},
]
Related
I have the project like this:
├── manage.py
├── myProjet
│ ├── __init__.py
│ ├── settings.py
│ ├── templates
│ ├── urls.py
│ ├── wsgi.py
│ └── wsgi.pyc
├── app1
├── templates
When I run the project, I am always getting this error: TemplateDoesNotExist at/
I have tried everything, but I can't fix it. My settings.py file is this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
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',
],
},
},
]
I have tried many ways, but I am always getting errors.
The error is raised on the signup function. The function is this:
def SignupPage(request):
if request.method=='POST':
uname=request.POST.get('username')
email=request.POST.get('email')
pass1=request.POST.get('password1')
pass2=request.POST.get('password2')
if pass1!=pass2:
return HttpResponse("Your password and confrom password are not Same!!")
else:
my_user=User.objects.create_user(uname,email,pass1)
my_user.save()
return redirect('login')
return render (request,'signup.html')
UPDATE ERROR
TemplateDoesNotExist at /
signup.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 4.1.6
Exception Type: TemplateDoesNotExist
Exception Value:
signup.html
Exception Location: /home/myPC/myProject/my_env/lib/python3.8/site-packages/django/template/loader.py, line 19, in get_template
Raised during: app1.views.SignupPage
Python Executable: /home//myProject/my_env/bin/python
Python Version: 3.8.10
Python Path:
['/home/myPC/myProject/pmTools_V1',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/myPC/myProject/my_env/lib/python3.8/site-packages']
Since django is using pathlib I'd go with:
"DIRS": [
BASE_DIR / "templates"
],
in your settings.py templates section.
Try to replace:
'DIRS': ['templates'],
By:
'DIRS': [os.path.join(BASE_DIR, 'templates')]
in TEMPLATES section.
Update
Try:
from django.http import HttpResponse
from django.template import loader
def SignupPage(request):
template = loader.get_template('signup.html')
# Your code here
return HttpResponse(template.render(context, request))
I'm trying to fork default oscar's static. My folder structure is the following:
myproject/
static/
oscar/
templates/
flatpages/
oscar/
myproject/
And I set the following settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'static/'
]
Forking templates works just fine, but the same thing doesn't work with static, default files are still served in HTML. To fork static I used this command:
./manage.py oscar_fork_static
Any idea why is it so?
Add this inside templates in settings.py file
'libraries' : {
'staticfiles': 'django.templatetags.static',
EX:
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',
],
'libraries' : {
'staticfiles': 'django.templatetags.static',
}
},
},
]
}
See if this is helps you
I called a template in the django render function, but django cannot find it
here is the django view code:
from django.shortcuts import render
# Create your views here.
def home_page(request):
return render(request, "index.html")
And after this I tried to check my settings.py and urls.py, but found nothing, and the file is also in the tamplate subfolder of the app
here is the directory structure:
│ .gitignore
│ db.sqlite3
│ funcational_test.py
│ manage.py
│
├─Terminal_equipment_management
│ │ asgi.py
│ │ settings.py
│ │ urls.py
│ │ view.py
│ │ wsgi.py
│ │ __init__.py
│ │
│ └─__pycache__
│
└─webui
│ admin.py
│ apps.py
│ models.py
│ tests.py
│ views.py
│ __init__.py
│
├─migrations
│
├─templates
index.html
the settings.py is:
INSTALLED_APPS = [
"api",
"webui",
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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',
],
},
},
]
urls.py is:
from django.contrib import admin
from django.urls import path, include
from webui import views
urlpatterns = [
path("", views.home_page, name="home_page"),
]
But when I visit localhost:8000/, Django will report TemplateDoesNotExist at /
here is some Error Message:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\ProgramData\Miniconda3\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\ProgramData\Miniconda3\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)
When I copied the files to the above two folders, the template took effect.
I can't be sure what happened
By Google Translate
Add Template DIRS:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / "template"],
'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 forgot to put s for template folder names
└─webui
│
├─tamplates
index.html
First, tamplates should be templates. Second, django looks inside a folder under templates for the app name. It seems redundant, and I agree, but that caused me issues when I was first using django.
└─webui
│
└─templates
│
└─webui
│
└─index.html
Before anyone asks, I've checked a lot of answers, and 'rest_framework' is listed on my Installed Apps. I have created a new venv and installed all dependencies on it again and still the same error.
When I try the url http://127.0.0.1:8000/admin I get the error template TemplateDoesNotExist at /Admin. When I try any endpoint of my API I get the same error.
I'm a bit confused because this project was running on my computer 1 month ago, but I got a problem in my computer and had to format. When I cloned the project again, got this error.
I'm going to list some of my settings about directory folder, I'm having a bad feeling about it.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'build')],
'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',
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static'),
]
Folders structure is:
Appapi/
movies/
settings.py
urls.py
wsgi.py
imdb/
migrations/
migrationsfiles....
models.py
views.py
serializers.py
...
venv/
manage.py
README.md
requirements.txt
And some other files, like gitignore, procfile, etc.
Some of the errors traceback:
django.template.exceptions.TemplateDoesNotExist: index.html
line 47, in select_template
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
Edit:
Django version 2.1;
I don't have a templates directory, I'm using rest-framework to make an API, so I'm not dealing with html, css and js with Django.
Can you share the place of the template directory. if the templates directory in the Appapi/ or in each app's directory.
Django Version ?
I have suggest some changes in settings.py file.
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',
],
},
},
]
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)
add a directory templates in every app
Appapi/
movies/
settings.py
urls.py
wsgi.py
imdb/
templates/
migrations/
migrationsfiles....
models.py
views.py
serializers.py
...
venv/
manage.py
README.md
requirements.txt
may be you can check resetting the migrations may help you..
As far as I have seen in the Django documentation, to show a custom 404 page all you have to do is put a 404.html in the root templates directory.
So my project structure is:
django_project
|_ config
| |_ settings.py
| |_ urls.py
|
|_ templates
|_ base.html
|_ index.html
|_ 404.html
In settings.py I have the following settings for the templates:
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',
'contact.views.contact_info',
],
},
},
]
for the 'DIRS' parameter I also used os.path.join(BASE_DIR, "templates"). This had the exact same outcome.
I also used
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, "templates"),
)
This lead to a deprecation warning.
In urls.py I also did not really do anything special:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="index.html"), name="index"),
url(r'^admin/', admin.site.urls),
]
Template inheritance is working perfectly in other apps, so the templates directory is found.
In settings: DEBUG=False
When I enter a wrong url I get the Django default 'NOT FOUND' page.
What am I missing here?
you need override 'handler404' variable, add this to urls.py
from django.conf.urls import handler404
handler404 = 'your_app.views.404'
https://docs.djangoproject.com/en/1.10/topics/http/views/#customizing-error-views
I guess you have to specify route for your 404.html template, or use from django shortcuts 'get_object_or_404' to handle error 404.