when I change DEBUG = False my page CSS styles not work.
also, I set allowed hosts but no difference made.
ALLOWED_HOSTS = ['localhost']
I use djngo2.2 and here is part of my setting.py:
DEBUG = False
ALLOWED_HOSTS = ['localhost']
# Application definition
INSTALLED_APPS = [
'matab',
'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 = 'labsolutions.urls'
TEMPLATE_DIR= os.path.join(BASE_DIR,'media/template/')
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',
],
},
},
]
WSGI_APPLICATION = 'labsolutions.wsgi.application'
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR,'media/static/'),
)
STATIC_ROOT = os.path.join(BASE_DIR,'statcifiles')
If you are in developement and you want to serve the static while setting DEBUG=False then you need to launch the server using the insecure option
python manage.py runserver --insecure
Of course it is only valid for development
Related
I've read many answers but I don't know what am I missing.
I did the same steps as they are mentioned in Django Docs (https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#admin-overriding-templates) but nothing happened.
Here is my actual configuration :
INSTALLED_APPS = [
'app1.apps.App1Config',
'app2.apps.App2Config',
'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 = 'rida.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],#i modified this line
'APP_DIRS': False, #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',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]
}
}
]
the contents of admin.py file of app1 :
from django.contrib import admin
from django.conf.urls import url
from app1.models import Modele
admin.site.register(Modele)
I want to override the template base.html, so I copied it from django\contrib\admin\templates\admin and put it in myproject\templates\admin\app1, then I made some changes on this file but nothing happened, so I have no idea why it doesn't work and what am I missing.
thank you in advance.
I tried serving my static files using whitenoise, but still not getting any of my css/js loading in my pages.
I ran collectstatic after enabling whitenoise and static folder was generated.
I tried using both
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
and django's ManifestStaticFilesStorage backend.
Here are my settings
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.humanize',
'crispy_forms',
'classroom',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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_school.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',
],
},
},
]
WSGI_APPLICATION = 'django_school.wsgi.application'
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'public/static'),
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
This is how my project is structured
home
-->etc
-->thegradientboostmvp
---->classroom
---->django-app
---->public
>static
>admin
>css
>img
>vendor
>second
>third
>fourth
---->static
------>admin
------>css
------>img
------>second
>css
>js
>img
------>third
>css
>js
>img
------>fourth
>img
>js
>css
---->templates
in my case putting my static folder in public_html solved.
I have run the python manage.py runserver command on my computer(windows 10) and it work but when I put all of codes on the google cloud platform compute engine(Ubuntu 16) and run the same command it shows all of the static files are not found. Do anyone knows how to fix it?
settings.py (ignore some irrelative parts)
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__)))
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework_swagger',
'restaurants', # 餐廳APP
'webpack_loader', # 整合vue和django套件
'corsheaders' # 處理跨域請求套件
# 'gunicorn', # 部署用
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'foodies.urls'
TEMPLATES = [
{
'BACKEND':
'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '../static')],
'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 = 'foodies.wsgi.application'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '../frontend/dist'),
)
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': '',
'STATS_FILE': os.path.join(BASE_DIR, '../webpack-
stats.json'),
}
}
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
# 允許所有header請求
CORS_ALLOW_HEADERS = ('*')
static files path
index.html
url.py
from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
from django.views.generic import TemplateView
schema_view = get_swagger_view(title='Pastebin API')
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('restaurants.urls')),
path('api-auth/', include('rest_framework.urls')),
path('api-view/', schema_view),
path('', TemplateView.as_view(
template_name="index.html"), name='index')
]
chrome error messages
compute engine status
I would suggest reading this:
https://docs.djangoproject.com/en/2.1/howto/static-files/deployment/
It has specific and exact instructions on the expected deployment of static files.
I'm stucked on this problem. I've checked many advices here and on google but no way to get rid of this error.
It seems that Django does not check app/templates/* directory. It should be checked automatically, shouldn't it?
My settings.py:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
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/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'sekret-key-goes-here'
# 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',
'chart_tools',
)
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 = 'gw_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 = 'gw_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
And here is the structure of the project:
Here is the error, as you can see, it does not check templates directory...
You need to add gw_app to your INSTALLED_APPS...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'chart_tools',
'gw_app',
)
my settings file is:
I create message files for the language kk-ar, kk-kz, kk-latn to translate. and these language codes for same langugae in defferent wrinting system. but that not suport in django settings, so I add it to settings, but when i run the server django not show my translation.
this why?
Generated by 'django-admin startproject' using Django 1.8.5 and Python
3.4
gettext = lambda s: s
BASE_DIR = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
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.locale.LocaleMiddleware',
'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 = 'task.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',
'django.template.context_processors.i18n',
],
},
},
]
WSGI_APPLICATION = 'task.wsgi.application'
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGES = (
('kk-kz', gettext('Kazakh')),
('kk-latn', gettext('Kazakh Latin')),
('kk-ar', gettext('Kazakh Arab')),
)
EXTRA_LANG_INFO = {
'kk-ar': {
'bidi': True, # right-to-left
'code': 'kk-ar',
'name': 'Kazakh Arab',
# unicode codepoints here
'name_local': u'\u0642\u0627\u0632\u0627\u0642\u0634\u0627',
},
'kk-latn': {
'bidi': False, # right-to-left
'code': 'kk-latn',
'name': 'Kazakh Latin',
# unicode codepoints here
'name_local': u'Qazaq',
},
}
# Add custom languages not provided by Django
laninfo = django.conf.locale.LANG_INFO
laninfo.update(EXTRA_LANG_INFO)
django.conf.locale.LANG_INFO = laninfo
# Languages using BiDi (right-to-left) layout
LANGUAGES_BIDI = global_settings.LANGUAGES_BIDI + ("kk-ar",)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'kk-ar'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = False
USE_TZ = True
and my url.py is like this:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
from .views import home, home_files
urlpatterns = [
url(r'^(?P<filename>(robots.txt)|(humans.txt))$',
home_files, name='home_files'),
]
urlpatterns += i18n_patterns(
url(r'^admin/', include(admin.site.urls)),
url(r'^$', home, name='home'),
url(r'^i18n/', include('django.conf.urls.i18n')),
)
There are three steps to making translations work in Django:
Marking the strings as translatable
Creating the PO files
Compiling the PO files to MO files
The localization guide here: https://docs.djangoproject.com/en/1.8/topics/i18n/translation/#how-to-create-language-files has a good step by step guide to the process.