I'm thinking in organizing all my templates in inside my project: scolarte.
As suggested by this question:
What is the best location to put templates in django project?
If you can’t think of an obvious place to put your templates, we
recommend creating a templates directory within your Django project
(i.e., within the mysite directory you created in Chapter 2, if you’ve
been following along with our examples).
But I need to call it from another app. The view is called but getting error:
TemplateDoesNotExist at /cuentas/ingreso/
scolarte/templates/scolarte/registration/signup.html
I even tried to put the full path to the template in project folder:
roles/views.py:
class SignUpView(TemplateView):
template_name = 'scolarte/templates/scolarte/registration/signup.html'
# don't work neither
#template_name = 'templates/scolarte/registration/signup.html'
#template_name = 'scolarte/registration/signup.html'
#template_name = 'registration/signup.html'
roles/urls.py:
from django.urls import include, path
from .views import SignUpView, SellerSignUpView, ClientSignUpView
urlpatterns = [
path('ingreso/', SignUpView.as_view(), name='signup'),
]
scolarte/urls.py
urlpatterns = [
path('', include('core.urls')),
path('cuentas/', include('roles.urls')),
path('admin/', admin.site.urls),
]
My app is orgnized like this:
roles
|_migrations
|_templates
...
|_urls.py
|_views.py
scolarte #project name
|_templates
|_scolarte
|_registration
|_signup.html
|_setting.py
|_urls.py
setting.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',
],
},
},
]
UPDATE 1:
roles app:
** roles app - view.py **:
UPDATE 2:
Your path to the template is incorrect by standard Django convention, but let me show you first how to fix it. What you'll want to do is make sure in settings.py you have these settings made. This is from a Django 3.0 fresh project creation.
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, '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',
],
},
},
]
If you use those, and follow the suggested directory structure of:
scolarte
|_scolarte
|_settings.py
|_urls.py
(etc..)
|_templates
|_scolarte
|_registration
|_signup.html
Then you can use with this path:
template_name = 'scolarte/registration/signup.html'
Related
My project structure is roughly as follows:
dinnerproject/
dinnerproject/
settings.py
dinners/
templates/dinners/
main.html
templates/
base.html
manage.py
In settings.py I've got TEMPLATE configured like so:
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',
],
},
},
]
And dinners app is added to INSTALLED_APPS.
I use a TemplateView with template_name = "main.html", which extends base.html.
But when I try to open the page that's supposed to return main.html, I keep getting a TemplateDoesNotExist error saying:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\User\dinnerproject\dinnerproject\templates\main.html (Source does not exist)
(...)
django.template.loaders.app_directories.Loader: C:\Users\User\dinnerproject\dinnerproject\dinners\templates\main.html (Source does not exist)
For some reason, my project name is duplicated in the paths, so django cannot find the right directories. What am I doing wrong?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'dinnerproject','templates'),
os.path.join(BASE_DIR,'dinners','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',
],
},
},
]
urls.py:
app_name="your app name"
This will solve your error
I am running a website using Django. There is no problem in login. When i logged in and click on some dashboard, it is showing "page not found"(404).
Views.py:
def index(request):
return(request,'obs_app/index.html')
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_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',
],
'libraries': {
'get_by_index':'obs_app.templatetags.templatefilters',
'get_by_key':'obs_app.templatetags.templatefilters',
'get_dict':'obs_app.templatetags.templatefilters',
'get_items':'obs_app.templatetags.templatefilters',
'multiple':'obs_app.templatetags.templatefilters',
},
},
},
]
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from obs_app import views
urlpatterns = [
url(r'index/',views.index , name = 'index'),
path('admin/', admin.site.urls),
path('', views.user_login, name='user_login'),
path('dashboard',views.obs_index, name='admin_dash'),
path('halls/active',views.obs_halls_active,name='active-halls'),
path('halls/pending',views.obs_halls_pending,name='pending-halls'),
path('febs/userlist', views.febs_user_list, name='febs-users'),
path('bookings/user', views.bookings_user, name='bookings-users'),
path('bookings/owner', views.bookings_owner, name='bookings-owner'),
path('cancellation/user', views.cancelled_user, name='cancelled-user'),
path('cancellation/owner', views.cancelled_owner, name='cancelled-owner'),
path('terms/obs', views.terms_conditions, name='terms-conditions'),
path('terms/febs', views.terms_febs, name='terms-febs'),
path('terms/febs/events', views.terms_febs_events, name='terms-febs-events'),
Error i am getting is :
Page not found (404)
The current path, index.html, didn't match any of these.
You have to render template and also fix the indentation
def index(request):
return render(request,'obs_app/index.html')
And also here you need to provide directory instead of file
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
Change:
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
To:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
{
...
'DIRS': [], # DIRS defines a list of directories where the engine should look for template source files, in search order.
...
},
]
You might want to read: Support for template engines
Related part copied here:
DIRS defines a list of directories where the engine should look for template source files, in search order.
I have encountered interesting problem on Django 2.
In my settings.py I have written this:
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',
],
},
},
]
After that I have created folder templates in my projects root folder.
Then I have created app with name front and added it into my settings.py INSTALLED_APPS:
Inside my views.py I have added this view:
def index(request):
return render(request, 'front/index')
I have assigned its url like this:
url(r'^$', views.index, name='index'),
When I'm trying to access to this view I get this error:
TemplateDoesNotExist at /
front/index
Why this happening? Did I miss something?
Change
def index(request):
return render(request, 'front/index')
to
def index(request):
return render(request, 'front/index.html')
I'm creating a project named "crepes_bretonnes". In this project, I have an application blog. I created a template date.html. Here is the structure of my folders :
crepes_bretonnes/
blog/
__init__.py
admin.py
migrations/
__init__.py
models.py
templates/
blog/
addition.html
date.html
tests.py
views.py
crepes_bretonnes/
__init__.py
settings.py
urls.py
wsgi.py
templates/
db.sqlite3
manage.py
When I try to see the page, I have a message templateDoesNotExist. I have read a lot about it on the web but I have not succeed in resolving my problem. In fact, I don't understand why Django does not search in my template folder of the app blog although I wrote "blog" in INSTALLED_APP in setting.py. Obviously, I have put TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),) in setting.py. I also tried to change the dictionary TEMPLATES. However, if I have understood well, it has no link as here Django should find my template even without this.
I don't have any solution.
Thank you for your help.
PS: If I put date.html in the general template folder and I arrange some lines, it works. However that is not a solution, I would like to respect a good structure.
UPDATE:
Thank you for your answer. Yes it really says INSTALLED_APPS in my setting and APP_DIRS is already True.
Here is my TEMPLATES in setting.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',
],
},
},
]
UPDATE:
The debug message shows that Django search in django.template.loaders.app_directories.Loader: /Users/benjamin/anaconda/lib/python2.7/site-packages/django/contrib/admin/templates/blog/date.html.
However I have not written anything in these folder ... I'm working in Documents. Why does Django search here and not in Documents ?
I solved it by adding forward slash "/" after 'templates':
'DIRS': [BASE_DIR / 'templates/'],
I had the same problem saying template does not exist and I solved it by changing the URL pattern in my views.py where previously I had mentioned just date.html, later I edited with blog/date.html. Hope it might help you.
I assume 'INSTALLED_APP' is a typo and it really says 'INSTALLED_APPS' in your settings. Anyway, you need to set APP_DIRS = True for templates to be found in app/templates
folders:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True, # this line is important
'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'
# ...
]
},
},
]
me having the same issue,
added the app to the INSTALLED_APPS
done, because you (and me) were using the templates not in the main app so it must be registered in the settings (like referring to the urls of the app from the main urls file)
Go to your setting.py and try something like:
os.path.join(BASE_DIR, "templates")
For example:
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',
],
},
},
]
For me, it was using rest_framework without adding it to INSTALLED_APPS.
So, here is my current structure:
django_project/
home_app/
__init__.py
migrations/
templates/
home_app/
base.html
admin.py
models.py
views.py
project_backend/
__init__.py
settings.py
url.py
wsgi.py
Now in home_app/views.py I have this very simple piece of code:
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = "home/base.html"
and in the settings.py I set up my templates like this:
import os
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, 'home_app', 'templates'),
# os.path.join(BASE_DIR, 'app_name', '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',
],
},
},
]
This works fine, and I am quite happy using the app/templates/app/some_temp.html approach, but this only works when I include the OS.path.joins for all the apps and templates, as far as I can see. Is there a way to register this structure in the settings by default and I am just overlooking it?
This is just no very pretty and I feel like it would be bad practice to write that down for every single app.:
os.path.join(BASE_DIR, 'home_app', 'templates')
You have 'APP_DIRS': True, so you shouldn't have to edit DIRS to use templates in the home_app/templates/ directory, as long as home_app is in your installed apps.
If you have a template,
home_app/templates/home_app/base.html
then you should use it with:
template_name = "home_app/base.html"
You currently have,
template_name = "home/base.html"
which isn't consistent.