templateDoesNotExist at / in django (what settings to do) - python

I'm new to django.
The error is coming
templateDoesNotExist at /
There is one app in my project,I've put the template folder in the app
Structure is like:
Project_name , manage.py,app_name,SQLite
The structure of the app (app_name):
other files...
Inside template folder .. (Templates(folder) -> app_name(folder) ->HTML file)
What setting I've to do in settings.py of prj to debug this error.
Did I've to include path of every template of each app. ?
Like inside setting.py in Templates
DIRS: ['BASE_DIR', "path"]

======== check & verify this ========
step-1 : check your app is registered or not
step-2: if your [templates] folder is in the app then go with the below setting otherwise put the path of your template in DIRS:['your template directory']
=== this is the default setting for a template if your templates folder in your application folder ===
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',
],
},
},
]
step-3: check in views.py template name is correct or not

Related

How to use template from project in another app?

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'

TemplateDoesNotExist error (Python 3.6.1, Django 1.11.1, PyCharm)

I tried to use render to show the HTML edited on PyCharm, but when I entered the address: 127.0.0.1:8000/index/, the following TemplateDoesNotExist exception appeared:
TemplateDoesNotExist at /index/
index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/index/
Django Version: 1.11.1
Exception Type: TemplateDoesNotExist
Exception Value:index.html
Exception Location: D:\python3.6.1\lib\site-packages\django\template\loader.py in get_template, line 25
Python Executable: D:\python3.6.1\python.exe
Python Version: 3.6.1
Python Path:
['C:\Users\Administrator\guest',
'D:\python3.6.1\python36.zip',
'D:\python3.6.1\DLLs',
'D:\python3.6.1\lib',
'D:\python3.6.1',
'D:\python3.6.1\lib\site-packages']
Server time: Fri, 2 Jun 2017 03:30:58 +0000`TemplateDoesNotExist at /index/
settings.py:
ROOT_URLCONF = 'guest.urls'
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',
],
},
},
]
views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,"index.html")
Screenshot of PyCharm:
I have tried some methods provided on StackOverflow, but they do not work. Is this exception caused by the wrong dirs? How can I deal with such situation?
Look at this:
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',
],
},
},
]
Specifically
'DIRS': [],
You need to tell Django where to look for your templates. If you have a templates folder in your main project root. Then, add
os.path.join(BASE_DIR, 'templates')
This assumes your project has this structure:
root
app1
...
templates/
what we're referencing with os.path.join(BASE_DIR, "templates")
....
static/
what we're referencing with STATIC_ROOT = os.path.join(BASE_DIR, "static")
...
If you are placing templates inside of your apps. There are two things.
Your app inside your project will need this structure for the template folder
app name (folder)
templates (folder)
app name (folder)
(template files) e.g. - "base.html"
You will need to add this line
os.path.join(BASE_DIR, APP NAME, "templates")
Addendum: Undoubtedly you will run into this problem as well... so let's go ahead and cover it.
If you are extending / including templates which are in an app (not in the base template folder) you will need to reference where they are located:
Example: I have a template in "myapp/templates/myapp/template.html" and I want to include it.
Then, I'd do {% include "myapp/template.html" %} or if extending {% extend "myapp/template.html" %}. If they are in the base template folder then you can just reference them directly as "template.html"
I have the same issue. It was mainly caused if you didn't install your app in "settings.py" file
Just do this in your settings.py file and your error will be gone
INSTALLED_APPS = [
'<app name>.apps.<app name>Config',
]
In the place of paste your "app name"

Django - templatedoesnotexist

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.

Django TemplateView searching in wrong place

My Django structure is:
testing
contacts
__init__.py
templates
contacts
index.html
(additional modules)
testing
__init__.py
templates
testing
test.html
urls.py
(additional modules)
Inside of the main URL module, testing.urls, I have a urlconf that is as follows:
url(r'^testing/$', TemplateView.as_view(template_name='testing/test.html'))
The problem is, it keeps looking in contacts.templates.contacts for the test.html file. With the existing urlcon, the debug page says the following:
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/admin/templates/testing/test.html (File does not exist)
/Library/Python/2.7/site-packages/django/contrib/auth/templates/testing/test.html (File does not exist)
/Users/*/Developer/django/testing/contacts/templates/testing/test.html (File does not exist)
It always defaults to the..........................^^^^^^^^^^ contacts folder for some reason. Is there some other parameters for TemplateView or template_name that can control this? Any help appreciated!
Update - Inside settings.py
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',
],
},
},
]
The testing/testing directory is not currently being searched by Django. The easiest fix is to add it to the DIRS setting:
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
Another option would be to add testing to your INSTALLED_APPS setting. Then Django would find your template, since you have APP_DIRS=True. However I wouldn't recommend this, because in your case testing/testing is the special directory that contains the settings.py and root url config.
By specifying "APP_DIRS": True, you are telling django to search for template files inside each app installed. Check in settings.py whether all your apps are contained within INSTALLED_APPS. If they are, you can try to force django to look for the templates in your app.
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
....
},
]

django view render to template from another app

I'm trying to render to a template from another app but not sure how to import or if I should be importing with templates?
The structure is
project->
main app->
templates->
pages->
home.html
account ->
current app->
views.py
my views.py file sits within the current app and is trying to access templates within the main app (in the pages subfolder).
How would I render to it:
def temp_view(request):
....
return render(request, "home.html",context)
At first you should have most probably configure your templates static path in settings.py with something similar to 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',
'django.template.context_processors.i18n',
],
},
},
]
APP_DIRS=True means Django will look for templates in each app directory in your case main_app/ and current_app/:
you have simply to mention the template path considering this as root path, so simply:
def temp_view(request):
....
return render(request, "pages/home.html",context)
Django will look under main_app/ directory to find the file pages/home.html
Would it be simpler to create the view in the same app you would like to render?
from mainapp.models import mainappmodel
def currentappview(request):
all = mainappmodel.objects.all()
return render(request, "pages/home.html", {'all': all})

Categories