I am new to python and working through a Zero to Hero course by Mosh on YouTube. I am getting stuck on a problem, and even after going through all the Django documents (https://docs.djangoproject.com/en/3.2/howto/overriding-templates/) I just can't see where I am going wrong. I have tried pasting the file path for the base.html file directly, as well as other methods such as os.path.join(BASE_DIR, 'templates') to try find the right path. But I still get an error.
Here is what I think all the code someone would need to understand the problem. Sorry if I have copied too much, but I am not sure what I actually need to show for someone to fully understand where I might have gone wrong.
The overall project is call PyShop and I have a file called index.html which is located in PyShop\products\templates\index.html which is trying to call the base.html file by {% extends 'base.html' %}. The base.html file is located here PyShop\templates\base.html.
When I run I get the below error. If I move the base.html file out from under the templates folder and just have it on it's own like this PyShop\base.html ie not in any subfolder then it works no problem.
TemplateDoesNotExist at /products/
Here is the PyShop\pyshop\settings.py file in which I have the directories etc for the web app.
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'products.apps.ProductsConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'pyshop.urls'
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',
],
},
},
]
Thanks for taking the time to look at it, it might be a pretty trivial mistake but I've no idea why it isn't working.
Related
In settings.py, DEBUG set to True. But when we move it to the production we should be setting DEBUG=False.
So in my local environment itself, I have changed to DEBUG=False. But still my application is working fine even with this settings. Usually when we changed DEBUG to false we should be getting some issues like 500 or 404 error something like that , but in my case its not like that.
I have referred this """ https://stackoverflow.com/questions/38617046/django-debug-false-still-runs-in-debug-mode/47266619""" but it did not help much for me.
Please let me know if i misunderstood or missed something.
Below is the small snippet of code i have in settings.py
import os
BASE_DIR =
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
DEBUG = False
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'MyApp',
'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': [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',
],
},
},
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Please let me know if i need to provide anymore details.
The purpose of DEBUG is to control what happens when the application encounters an error.
If that happens and DEBUG is true, then the error page will contain debugging information that is useful to developers.
But you would not want this information to be shown to real users, so DEBUG should be false when deployed to production.
Setting DEBUG to false does not cause errors, as you seem to think.
Currently I just moved my django project to a new ubuntu server. But when I tried to access the admin website, it always return "TypeError at /admin/: expected str, bytes or os.PathLike object, not PosixPath". The environment of my previous server is: Python3.6.9 and django 3.0.4. The current versions are Python 3.8 and Django 3.2.5. Are there any changes after the version update?
The settings.py is:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXXX'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'MyBlog',
...
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
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',
'MyBlog.context_processor.Alert_stat',
],
},
},
]
WSGI_APPLICATION = 'MyBlog.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myweb',
'USER':'root',
'PASSWORD':'112321',
'HOST':'localhost',
'PORT':'3306',
'OPTIONS':{'charset':'utf8mb4'},
}
}
...
STATIC_URL = '/static/'
STATICFILES_DIRS = [(
os.path.join(BASE_DIR,'static')
)]
#templates dirs
TEMPLATES_DIRS = (os.path.join(BASE_DIR, 'templates'))
#Media dirs
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I noticed that the errors are induced by the version differences between Django 3.2.5 and Django 3.0.4. The main issues come from the path settings in settints.py. If someone faces the similar problems maybe these Q&As will be helpful:
Django TypeError: argument of type 'PosixPath' is not iterable
"TypeError at / argument 1 must be str, not PosixPath" in django ecommerce website
TypeError: expected str, bytes or os.PathLike object, not tuple in Django
My solution is to downgrade django to 3.0.4.
I am working with this app, I have tried running the app using the command provided in the doc then I had to install both Django and requests but now the template view do not render at all, it seems it's interpreted as text
What do I have to install? Or this app is not working properly? I am not sure why there's no requirements.txt
Below is the settings file
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'xxxxxxx'
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Company.urls'
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',
],
},
},
]
There's also a file named urls.py that contains the path I am supposed to work with
urlpatterns = [
url(r'^payment_request/',Payment_Request),
url(r'^payment_response/',Payment_Response),
url(r'^payment_receipt/',Payment_Receipt),
url(r'^admin/', admin.site.urls),
]
Only admin is working, Payment_Receipt and Payment_Response shows error but that's normal because they should get some data to process before
I also I'd like to mention that in the docs they ask to generate some hash sequence, is that mandatory to simply shows the htm template? I have never done this before
Create SHA256 Hash with below mention Parameters.
Merchant needs to form the below hash sequence before posting the transaction.
Below is the SHA 256 Hash creation format : Hash Sequence :-
trackid|Terminalid|password|secret_key|amount|currency_code
There are two ways this could happen:
Content type is wrong - Open up the Chrome Developer Console, Go to Network tab, reload the page and look at the content-type header in the request. Is it text/html? If it's something else, Chrome won't render it
The HTML is escaped - Right click on the page and click View Source. Is it different from the HTML you see in the page?
You have to add the name of the app in settings.py in the list INSTALLED_APPS
INSTALLED_APPS = [
"appname",
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
I recently spun off an old project and tried installing its apps (which worked) and also installing django-helpdesk/bootstrap_forms. But django could find neither helpdesk templates, or bootstrapform templates. My settings.py looks like:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'key'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'index',
'taggit',
'helpdesk',
'bootstrapform',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
HELPDESK_PATH = "/home/cchilders/.local/virtualenv/new_bookmarks/lib/python2.7/site-packages/helpdesk/templates",
BOOSTRAPFORM_PATH = "/home/cchilders/.local/virtualenv/new_bookmarks/lib/python2.7/site-packages/bootstrapform/templates"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates"),
"/home/cchilders/.local/virtualenv/new_bookmarks/lib/python2.7/site-packages/helpdesk/templates",
"/home/cchilders/.local/virtualenv/new_bookmarks/lib/python2.7/site-packages/bootstrapform/templates",],
'APP_DIRS': False,
'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_ROOT = '/home/cchilders/django_practice/new_bookmarks/static'
STATIC_URL = '/static/'
I don't have an issue now, but am very confused as to why I had to manually do HELPDESK_PATH and BOOSTRAPFORM_PATH. The last time I spun this up, installing the apps worked out the box, with the same TEMPLATE configs. I have these installed:
Django==1.8.3
argparse==1.2.1
betterpath==0.2.2
django-bootstrap-form==3.2
django-extensions==1.5.5
django-helpdesk==0.1.16
django-markdown-deux==1.0.5
django-mptt==0.7.4
django-taggit==0.16.2
email-reply-parser==0.3.0
ipython==3.2.1
l==0.3.1
lxml==3.4.4
markdown2==2.3.0
pytz==2015.4
requests==2.7.0
simplejson==3.8.0
six==1.9.0
vcversioner==2.14.0.0
wsgiref==0.1.2
zope.interface==4.1.2
I am running a VE (like I was when it found templates) and packages are where expected:
~/.local/virtualenv/new_bookmarks/lib/python2.7/site-packages/bootstrapform/templates
When turning off the new URLS back to 'DIRS': [os.path.join(BASE_DIR, "templates"), ], I get
TemplateDoesNotExist at /helpdesk/
helpdesk/public_homepage.html
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/cchilders/django_practice/new_bookmarks/templates/helpdesk/public_homepage.html (File does not exist)
and I don't get why it isn't checking the usual places. My VE works, if I deactive it I can't runserver due to missing packages like 'taggit'. What usually causes django to ignore site-packages and not look for templates beyond BASE_DIR/templates? Thank you
** I'm using Django 1.8. The templates feature has changed in this release of Django. Read more here Upgrading templates to Django 1.8**
This is bothering me because I've come across this issue and fixed it for one of my other projects, but I can't for the life of me figure out how to fix it this time around. I've gone through countless stackoverflow questions and tried to resolve the issue using the answers provided by I've had no luck. This is the error message I am getting:
Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Exception Location: /Library/Python/2.7/site-packages/django/template/loader.py in get_template, line 46
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/Users/User1/Documents/PyCharmProjects/Project1',
It seems that is it looking in the wrong folder, it should be looking under Project1/templates according to my settings.py file:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, '/templates/')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template.loaders.eggs.load_template_source',
)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'Project1.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',
],
},
},
]
My templates folder is in the root folder of my project. What's the issue here? I have given it a TEMPLATE_DIRS parameter, and used a proper BASE_DIR, which is what the majority of the answers recommend.
remove the slashes: TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
See here
Things have changed with Django 1.8, in which the template system has been improved. See the release notes.
In your settings.py add:
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',
],
},
},
]
the code above comes straight from one of my projects.
Feel free to use os.path.join(BASE_DIR, 'templates') instead of catenating the strings.
1) If your route for templates is project_name/app_name/templates/app_name
'DIRS': [os.path.join(BASE_DIR, 'app_name', 'templates', 'app_name')],
2) If your route for templates is project _name/templates or project_name/app_name/templates_only
'DIRS': [#leave it just empty, will work fine],
Note: 'template_only' means that templates/*.html there is no any folder inside templates.
I would try placing TEMPLATE_PATH in DIRS:
TEMPLATES =[
{....
'DIRS' : [TEMPLATE_PATH,],
....