Trouble overriding django/grappelli base.html template - python

According to the Django tutorial I make a templates directory under BASE_DIR, and add both an admin and an Grappelli subfolder, later copying base.html from both and putting each in its respective directory. Then I make some changes, add some CSS and JS... Reload the test server but no change is reflected at my admin interface, even the CSS/JS I add is not there!
my TEMPLATES in settings.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',
'django.core.context_processors.request',
],
},
},
]
i must be doing something utterly wrong, but i have no idea what and the documentation isn't helping.
ANSWER: The problem was the 'DIRS': [] only working in django 1.8+, for Django 1.7- we need to use the following:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)

my settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__) + '/../')
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
'apps.projects.context_processors.status',
)
SITE_ID = 1
ROOT_URLCONF = 'apps.urls'
WSGI_APPLICATION = 'apps.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
STATIC_URL = '/static/'
STATIC_ROOT = PROJECT_DIR + '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = PROJECT_DIR + '/media/'
TEMPLATE_DIRS = (
PROJECT_DIR + '/templates/',
)

Related

Why am I getting a TemplateDoesNotExist error in Django 4.0 when the template does exist?

I'm new to Django, and I've been struggling with this TemplateDoesNotExist error for a while. I looked at many other users who were having these issues, but none of the proposed solutions seem to work for me.
Django cannot find my blog/base.html file and claims it looks in '/home/agile/django_project/blog/templates/blog/base.html' for it, but when I check in my ubuntu, it says the directory exists fine?
When I run on my localhost, the site works fine, but when working with ubuntu and Linode is when I run into this problem.
Image of Django error
Image of the path that exists but Django can't find?
Again, I have tried many 'solutions' by adding to template DIRS and stuff but honestly, none have worked. I just don't know what is wrong/how to go about fixing it. Any help I would greatly appreciate.
UPDATE:
I was able to fix it. I accidently put "blog/base.html " instead of "blog/base.html"
Here is my settings.py file, too:
Django settings for django_project project.
Generated by 'django-admin startproject' using Django 3.2.8.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
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.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-e5oqam$0o(1158xprooll%5dg)xk01nw8alh6z1cv#)674^48k'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ["45.56.100.252"]
# Application definition
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'users.apps.UsersConfig',
'crispy_forms',
'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.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'django_project.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',
],
},
},
]
WSGI_APPLICATION = 'django_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
#using this method makes sure full path to directory is created correctly no matter OS.
#BASE_DIR means media root will be at projects base directory.
#media is where uploaded files will be placed
#media url is how we access that image in the browser
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
LOGIN_REDIRECT_URL = 'blog-home'
LOGIN_URL = 'login'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '...'
EMAIL_HOST_PASSWORD = 'fiftftlyutyokgnu'```
in settings.py use this in you templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'blog')],
'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 admin has no design in live server

I uploaded my whole site in my live server after uploading django admin is not showing any design before uploading it was fine but after upload in it got like this :-
Here is my settings.py I think I did all the thing perfectly but still :( I tried so many time and I got stuck here almost 2 days :) I hope someone will help me with this problem.
Settings.py
"""
Django settings for gng project.
Generated by 'django-admin startproject' using Django 3.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
import os
# 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.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '$tl4x!&*01z(0o2r&la&e#)f0dz0x)^bov7dh^aau$m#shktiw'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['gngbd.xyz', 'www.gngbd.xyz']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'store',
]
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 = 'gng.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',
],
},
},
]
WSGI_APPLICATION = 'gng.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
DEBUG = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static'] # Main Line #
STATIC_ROOT = os.path.join(BASE_DIR, 'static/css/')
STATIC_ROOT = os.path.join(BASE_DIR, 'static/js/')
STATIC_ROOT = os.path.join(BASE_DIR, 'static/fonts')
STATIC_ROOT = os.path.join(BASE_DIR, 'static/admicss')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "images/"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, 'staticfiles'))
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'gngxyz/static'),
)
I think you have to run following command to collect all static files in one place:
python manage.py collectstatic
then press ctrl+F5. I hope this solves your problem.
And check if you have following in your settings.py:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, 'staticfiles'))
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
In addition to collecting your static files, in some web servers you have to reconfigure location of your static files. For example in nginx you have to add the following :
location /static {
autoindex on;
alias /direction/to/your/static;
}

CSS not working with html file in django, why is it not working?

My CSS file does not seem to be working in Django and I am not sure why. I have tried many different ways to get it to work but it's still not working. I have added {%load static%}on top of the HTML file and my css file is named main.css which is in a folder called static. I have also added the line
<link rel="stylesheet" type= "text/css" href="{% static 'css/main.css' %}" >
to my html file. Also in my setting.py file I have added this line
STATICFILES_DIR=[
"/Users/yaminhimani/Desktop/tweetybird/static",
]
in order to find the CSS file. After doing all this why would the styling still now show up for my website?
Check this approach and see if it can help you. Django v3
settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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',
],
},
},
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
This may already be in your settings.py file but it may be worth it to check:
STATIC_URL = '/static/'
If that's good, then it could be the folder setup. The Django documentation recommends nesting the CSS files in the following way:
app_name/static/app_name/main.css

Django TemplateDoesNotExist problem while DIR is correctly set

I was running this Django project perfectly previously using Python3.7.2 and Django 2.1.5, recently I installed Anaconda3 and changed the interpreter to the Python3.7.1 in it. After reinstalling Django, when I ran the project the TemplateDoesNotExist exception showed up. My OS is Windows 10.
My DIRS is set as below:
TDIR = os.path.join(BASE_DIR, 'Templates/').replace('\\', '/')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TDIR],
'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 note is on the error page:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\ProgramData\Anaconda3\lib\site-packages\django\contrib\admin\templates\welcomePage.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\ProgramData\Anaconda3\lib\site-packages\django\contrib\auth\templates\welcomePage.html (Source does not exist)
It seems that Django search the Anaconda path for templates instead of the customized path, but I can see on the error page that it was correctly set. This is what the error page shows:
TEMPLATES
[{'APP_DIRS': True,
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['D:/IgnorazWork/SUDoc/SUDoc/Templates/'],
'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'D:/IgnorazWork/SUDoc/SUDoc/Templates/'is exactly where I put the templates. How does this happen?
Thanks!
Try this :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Note - Make sure that your folder name are exactly same Template\ and template\ are different

Looking for template in a wrong directory

I have uploaded my Django project to Openshift and python code works correctly, however, my templates are being loaded from a wrong folder:
/var/lib/openshift/55b9********************/templates
My settings.py file contains:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
which correctly points to
/var/lib/openshift/55b9********************/app-root/runtime/repo/wsgi/marko/templates
As shown in django's traceback page. Why could this be happening? I could copy my templates to the folder it looks for, but I'd rather not place any project files outside of the project folder. runserver on local machine looks for templates in correct folder.
Are you running Django 1.8? The TEMPLATE_DIRS directive is deprecated and replaced by TEMPLATES.
According to the docs you should update your settings like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'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',
],
},
},
]
Assuming BASE_DIR/templates points to the right directory.

Categories