Django templates does not exist - django

So I work on windows and I try to load my websit home page. However I get the ''Templates doesnot exist '' errors I don't know why,
TemplateDoesNotExist at /
home/home.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.10.1
Exception Type: TemplateDoesNotExist
Exception Value:
home/home.html
Exception Location: C:\Python35\lib\site-packages\django\template\loader.py in get_template, line 25
Python Executable: C:\Python35\python.exe
Python Version: 3.5.2
Python Path:
Here is my Tracerback
you can see there that Django cannot even load some templates to be tested with. I'v seen others who have the same problem but at leat there was a list of tried template me I don't even have this.
here is my home/views.py
class IndexView(View):
template_name = 'home/home.html'
# not logged in
def get(self, request):
return render(request, self.template_name)
here is my templates variable in my setting.py
TEMPLATE = [
{
'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 my code and my structure is
this and you can see that I added Home in the installed_app
yet after changing the path root of project, changing my python version (3.5 instead of 2.7) put a templates folder on the same level of my manage.py and change APPS_DIR to false i still get nothing. I have try to know if it was a path problem so I put the file in the root of project and test with isFile function of python and the return was true but if I try to render the page the same link I get the error.So I don't know if it's my code, my configuration of my project(virtual environnement, django etc) and It really gets annoying I use pycharm and I'm on windows 8.1

You could try with this path in settings.py to properly call the template:
'DIRS': [
os.path.join(PROJECT_ROOT, 'templates').replace('\\','/'),
],

You are not supposed to place your apps on an apps package. Try replacing your applications architecture to something like
INSTALLED_APPS = [
'applications.events',
'applications.student',
'applications.resume',
'applications.home`,
]
instead of "something.apps.somethingelse".
that is Pythonic.

Related

Django throws TemplateDoesNotExist for a template located it in the 'my_app/templates/my_app' subfolder

I am studying Django and got stuck right at the first project.
I've got TemplateDoesNotExist exception when accessing index page of my application. I saw a lot of same looking questions here, but answers seem to be referring to slightly different cases or older Django versions.
Template-loader postmortem shows that Django looked for the index page (index.html) in the 'movies/templates' directory, while file is placed inside 'movies/templates/movies' as it is suggested by manuals.
Project info
Project structure
'movies' is added to the INSTALLED_APPS list in the settings.py
And movies/views.py refers to the index.html in following way:
def all_films(request):
return render(request, 'index.html', {'movies': my_movies, 'director': settings.DIRECTOR})
What worked
Adding 'os.path.join(BASE_DIR, 'movies', 'templates', 'movies'),' to the TEMPLATES['DIRS'] values in the settings.py makes it work, but after reading manuals and answers to same questions I assume it shouldn't be required.
Also, if I change movies/views.py so that it refers to 'movies/index.html' instead of 'index.html' everything works. But is it a good practice to use app name in links like this? I mean, it could be hard to maintain in future etc.
Question
Basically, question is what am I doing/getting wrong.
Could you please suggest, what else should I check to make it work in default state without manual editing of TEMPLATES' DIRS property?
P.S. I am using Django 3.2.7, OS is Ubuntu.
Check if 'APP_DIRS': True in settings.py>TEMPLATES
This should work!
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
views.py
def all_films(request):
return render(request, 'movies/index.html', {'movies': my_movies, 'director': settings.DIRECTOR})

Django TemplateDoesNotExist at /rango/

I've seen this question asked before, but I'm already using the newer TEMPLATES data structure which is the common solution.
From the tango with django book, I'm currently on unit 4. When I run my program with python manage.py runserver
I receive the following error:
TemplateDoesNotExist at /rango/
rango/index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/rango/
Django Version: 1.5.1
Exception Type: TemplateDoesNotExist
Exception Value:
rango/index.html
Here is the relevant code in my ~/Workspace/wad2/tango_with_django_project/setings.py file:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'tango_with_django_project', 'templates')
.
.
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',
],
},
},
]
.
.
Also, here is the relevant code in my ~/Workspace/wad2/rango/views.py file:
def index(request):
context_dict = {'boldmessage': "Crunchy, creamy, cookie, candy, cupcake!"}
return render(request, 'rango/index.html', context_dict)
Also, I cannot solve this problem with a direct path since I am trying to stick to the tutorial with dynamic pahs. Lastly, my template is located at ~/Workspace/wad2/tango_with_django_project/templates/rango/index.html
This was solved by updating my django version with:
pip install django==1.10.5

Django: Context processor error for anonymous user

Hi I have a model which stores users actions. This model is used for showing notifications to logged in users.
Now the issue is code works fine with logged in users, however if I open homepage of application after logout, following error appears.
TypeError at /
'AnonymousUser' object is not iterable
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.10
Exception Type: TypeError
Exception Value:
**'AnonymousUser' object is not iterable**
Exception Location: /Library/Python/2.7/site-packages/django/utils/functional.py in inner, line 235
Python Executable: /usr/bin/python
Python Version: 2.7.10
I think the issue is because of template context processor I am using in my application.
Please help on this.
context code
from models import notifications
def activ_notification(request):
active = notifications.objects.filter(to_user=request.user,viewed=False)[:10]
return({'alert':active})
Settings
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',
'task.notification.activ_notification',
],
},
},
]
Try just add is_authenticated check inside your view:
def activ_notification(request):
active = []
if request.user.is_authenticated():
active = notifications.objects.filter(to_user=request.user,viewed=False)[:10]
return({'alert':active})

Django context processor constants work in dev but not in production

I have a context processor which makes constants available in templates, including base template.
On my vagrant development server, using runserver, these work fine. The production server uses apache and WSGI.
I'm using Django 1.9.4 on both development and production.
This is the context_processors.py
from .constants import *
def template_constants(request):
return {'collection_name': COLLECTION_NAME,
'website_name': WEBSITE_NAME,}
This is from settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.request',
'django.template.context_processors.debug',
'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',
'myapp.photos.context_processors.template_constants',
],
},
},
]
Constants are just in a file constants.py in my app, which I import into files which use them. The file has lines such as:
WEBSITE_NAME = "website name"
My views use render which should be including RequestContext
return render(request, 'page.html', template_variables)
I get no error messages, the variables simply don't have a value in the templates on production, so nothing is shown.
Apart from deployment, the code on production and dev is exactly the same.
Things I've tried:
- Adding string value directly into context_processors.py file (eliminating it being a potential issue importing variables from constants.py)
- Going back to Django 1.8 and 1.7 to check if it's something related to a specific version
None of these changes made any difference.

Template Does not exist

I am new to Django. I use pydev eclipse as an IDE. First I created a project then an application welcome on that project. I made a folder named Templates within the project and make a file "home.html" and home.html contains
<div>
This is my first site
</div>
I modify the settings.py file as
TEMPLATE_DIRS = ("Templates")
INSTALLED_APPS = (
..........#all default items
'welcome', #the added one
)
views.py includes
from django.shortcuts import render_to_response
def home(request):
return render_to_response('home.html')
urls.py contains
from django.conf.urls import patterns, include, url
from welcome.views import home
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'MajorProject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', home),
)
then I run it as django project and open my browser and see on localhost:8000/home
it shows error
TemplateDoesNotExist at /home/
home.html
Request Method: GET
Request URL: http://localhost:8000/home/
Django Version: 1.6
Exception Type: TemplateDoesNotExist
Exception Value:
home.html
Exception Location: C:\Python27\django\template\loader.py in find_template, line 131
Python Executable: C:\Python27\python.exe
Python Version: 2.7.2
Python Path:
['D:\\Bishnu\\BE\\4th year\\8th semester\\Major Project II\\Working\\Workspace\\MajorProject',
'C:\\Python27\\lib\\site-packages\\distribute-0.6.35-py2.7.egg',
'D:\\Bishnu\\BE\\4th year\\8th semester\\Major Project II\\Working\\Workspace\\MajorProject',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode',
'C:\\Windows\\SYSTEM32\\python27.zip']
Server time: Sun, 2 Jun 2013 14:25:52 +0545
Try to set Templates Directory on setting.py.
as
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__),'templates'),
)
If you're using Django 1.8+
You'll get this warning:
(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, TEMPLATE_DEBUG.
Add your template directory to the Base TEMPLATES setting under the DIRS dictionary
Like so:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
root("templates"), #### Here ####
],
'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 1.9
in settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR+r'\templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
],
},
},
]
Directory with templates should be named templates, not Templates (even though on windows it may be the same). Also make sure, you have application in PYTHONPATH or the correct directory structure of your project and application like:
project/
project/
settings.py
...
welcome/
templates/
home.html
views.py
...
manage.py
Then you don't need to change TEMPLATE_DIRS because app_directories.Loader (enabled by default) will find the templates in your application.
Also of if you still want to change TEMPLATE_DIRS, use absolute paths, but preferred way is the app_directories.Loader.
check below steps to fix
step 1:
Inside templates, 'DIRS' : [ ], metion this:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
step 2:
Go in your views.py file and look for template_name (check its spelling and also check did you mentioned the right html file here or not)
step 3:
Check in your urls.py file wether you mentioned the right template name or not in the path
format: urlpatterns = [ path(" ", class name or function name, name = template name)
BASE_DIR = Path(file).resolve().parent.parent
This is default directory code by Django
And looks like this
C:\user\pc_name\django_project
But if you delete last .parent it will looks like this
C:\user\pc_name\django_project\django_project
So new BASE_DIR code which is this
BASE_DIR = Path(file).resolve().parent
Add this variable to TEMPLATE_DIR
TEMPLATE_DIR = os.path.join(BASE_DIR, 'template")
And last code define this
C:\user\pc_name\django_project\django_project\template
In the end safely uptade DIRS
'DIRS': 'TEMPLATE_DIR'
Hope you did