I know there are multiple questions on this site about this problem, but I cannot find a solution.
I am using Python 3.6 (anaconda) + django 2.0.2 on Windows 10.
I am following the tutorial: https://docs.djangoproject.com/en/2.0/intro/tutorial03/
Here is my views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from .models import *
def index(request):
content = 'abcxyz'
context = {'content': content}
return render(request, 'polls/index.html', context)
I created a file index.html in the folder polls\templates\polls
My settings.py:
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'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_site.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_site.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
I have a problem of 'TemplateDoesNotExist' - it seems to me that django tries to look for a template in
django.template.loaders.app_directories.Loader: /mnt/e/workspace/capec-processing/code/django_site/polls/templates/polls/templates/polls/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user_name/anaconda3/envs/capec/lib/python3.6/site-packages/django/contrib/admin/templates/polls/templates/polls/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user_name/anaconda3/envs/capec/lib/python3.6/site-packages/django/contrib/auth/templates/polls/templates/polls/index.html (Source does not exist)
I am not sure what did I do wrong, because I followed the tutorial on the django website.
Could you suggest me a hint?
Update
Here is the structure of the my root directory (called django_site):
django_site
--django_site
----settings.py
--polls
----templates
------polls
--------index.html
----views.py
--db.sqlite3
--manage.py
In your setting.py you need to add this
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls' # You need to add this too. This should be same as your app name.
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")], # Add this to your settings file
'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',
],
},
},
]
And your template folder should be here
myproject/
|-- myproject/
| |-- polls/
| |-- myproject/
| |-- templates/ <-- here!
| | |-- polls/
| | | |-- index.html
| | |-- base.html
| | +-- home.html
| +-- manage.py
+-- venv/
change value of DIRS under TEMPLATES to this to tell django to look for templates in your app directory.
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
and then you can either put templates parallel to manage.py or under specific app directory.
+app
-+__init__.py
-+templates
---+polls
-----+index.html
change your order of the installed apps in your settings
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig',
]
All you need to do is add the name of your app, i.e. polls, to the list of installed apps in the settings.py file and reload the URL to the app index page. I don't know why the Django tutorial still does not include this step as it trips everyone up, every time.
settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
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.
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
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've overriden Django-allauth templates in my Django project just creating a new folder "account" in templates folder. This works during deployment on Windows but when I moved the project to DigitalOcean VPS, it is looking for old templates (in allauth app) so I see old styled allauth pages.
This is the structure:
firstapp\
authapp\
templates\
account\
email.html
login.html
anotherapp\
I tried to switch allauth apps and my authapp in INSTALLED_APPS but it doesn't seem to work.
And this is my settings.py INSTALLED_APPS.
INSTALLED_APPS = (
'django.contrib.auth',
'mainapp',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',
'crispy_forms',
'super_inlines',
'django_tables2',
'quizzesapp',
'ordersapp',
'django_extensions',
'authapp',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
'languagesapp',
'widget_tweaks',
'localflavor',
'paypal',
'paypal.standard.ipn',
'nested_inline',
'django_forms_bootstrap'
)
What do you thing I should do?
EDIT:
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',
'django.core.context_processors.i18n',
# 'django.core.context_processors.request',
# "django.contrib.auth.context_processors.auth",
# "allauth.account.context_processors.account",
# "allauth.socialaccount.context_processors.socialaccount",
],
},
},
]