Can't load static files Django 1.9 - django

Template cant find static files and load it on page.
This is my settings:
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__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'cargo/templates')
# STATIC_DIR = os.path.join(BASE_DIR,'cargo/static')
DEBUG = True
ALLOWED_HOSTS = []
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 = 'mysite.wsgi.application'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = 'os.path.join(os.path.expanduser("~"), "domains/h2h.su/static/")'
This my path to files cargo/static/cargo/images and templates at the same path in cargo app cargo/templates/cargo/index.html
Can someone help me?

I've fixed the problem by changing os.path.join(BASE_DIR, "static"),
to os.path.join(BASE_DIR, "cargo"),

Related

Django template does not exist?

When I am trying to deploy to railway my project, i get the error mentioned above. This is one log that could give more insight?
django.template.loaders.filesystem.Loader: /app/client/client/public/index.html (Source does not exist)
Here is my project.settings.py file:
from pathlib import Path
import os
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'client')
],
'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 = 'project.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'game-api',
'HOST': 'localhost',
'PORT': 5432
}
}
ROOT_URLCONF = 'project.urls'
STATIC_URL = '/static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
project.views.py file:
from django.shortcuts import render
def index(request):
return render(request, 'build/index.html')
My files:

The directory '/static/' in the STATICFILES_DIRS setting does not exist. (Visual Studio)

When I want to "python manage.py makemigrations", it returns the following.
System check identified some issues:
WARNINGS
?: (staticfiles.W004) The directory '/static/' in the STATICFILES_DIRS setting does not exist.
How should I solve this issue?
from pathlib import Path import os BASE_DIR = Path(__file__).resolve().parent.parent ALLOWED_HOSTS = ['*'] INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'products', 'orders', ]
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 = ‘rtu_server.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 = ‘rtu_server.wsgi.application'
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } 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', }, ]
LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Asia/Hong_Kong' USE_I18N = True USE_TZ = True STATIC_URL = 'static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ] DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
It just says that you created STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] in your settings.py, but you didn't have any static folder in your project.
It is not a big issue right now because you might not be using external CSS or JS. But if you want to solve the issue, you can just create a static folder in your project.
So what you need to do here to make it easier for you is to navigate to the location where you have your project created and create a FOLDER called static. Don't try to do it for your IDE editor as you might end up getting confused. I am assuming you are trying to link up files such as CSS to your Django project. Do this and your problem will be solved.
Also, remember to load your static statement at the header of your HTML page.
As an example:
<link rel='stylesheet' href='{% static 'style.css' %}' type='text/css'>
{% load static %}
Create a folder "static" in root directory if you still see error, then.
The problem is with this i believe,
BASE_DIR = Path(__file__).resolve().parent.parent
Let's say this is tree of folder where your "settings.py" is:
E:\\Project>Project>mySettings>settings.py
Then you have to modify BASE_DIR little bit like below,
BASE_DIR = Path(__file__).resolve().parent.parent.parent
one .parent for each folder

Using django-environ; Django admin css not found

I removed the BASE_DIR and the os import because I am using django-environ.
However when I entered the admin dashboard of Django, the css is not read so I only see plain html elements.
Here is how part of my settings.py looks like.
import environ
root = environ.Path(__file__) - 3 # get root of the project
env = environ.Env()
environ.Env.read_env() # reading .env file
SITE_ROOT = root()
DEBUG = env.bool('DEBUG', default=False)
TEMPLATE_DEBUG = DEBUG
ALLOWED_HOSTS = tuple(env.str('ALLOWED_HOSTS', default=[]))
DATABASES = {
'default': env.db('DATABASE_URL')
}
SECRET_KEY = env.str('SECRET_KEY')
# Static and Media Config
public_root = root.path('public/')
MEDIA_ROOT = public_root('media')
# MEDIA_ROOT = root.path('media/')
MEDIA_URL = env.str('MEDIA_URL', default='media/')
STATIC_ROOT = public_root('static')
STATIC_URL = env.str('STATIC_URL', default='static/')
ROOT_URLCONF = 'myproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [root.path('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',
],
},
},
]
So I also put in root.path('templates') into the DIRS of TEMPLATES.
I also added this snippet below to the project's urls.py.
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
I also have this on my .env file:
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
MEDIA_URL=media/
STATIC_URL=static/
Where could I have gone wrong and what should be written instead?
I looked into djangogirls tutorial and found out that I missed out some slashes.
From my .env file:
MEDIA_URL=media/
STATIC_URL=static/
I changed it into
MEDIA_URL=/media/
STATIC_URL=/static/

Media Files not shown in DEBUG=False Django 2.2.6

Every thing works perfect in debugging mode but can't show the media files on production environment while debugging id false
That's my settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.template.context_processors.debug',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.csrf',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.static',
'django.template.context_processors.media',
],
},
},
]
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"django.core.context_processors.debug",
"django.core.context_processors.auth",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
)
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticroot')
# Media folder for database media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and the project urls
if settings.DEBUG:
urlpatterns += static(
settings.STATIC_URL,
serve,
document_root=settings.STATIC_ROOT
)
urlpatterns += static(
settings.MEDIA_URL,
serve,
document_root=settings.MEDIA_ROOT
)
Although the url link path shows perfectly using template tags
{% load staticfiles %}
<a href="{{ magazine.document.url }}" target="_blank">
<i class='far fa-file-pdf'></i>
</a>
Hope you can help to fix this issue.
It was my fault PDF files must be downloaded or viewed by third party viewer.. and for debugging mode I must have Apache server to work when debugging is False... Hope this help any one face the same issue

templets not found error in django

I am little bit confused about django templates, I have attached screen shot of workspace, can some one please tell how can I use addprofile.html template in my views.py in buddy app.
In my settings/base.py I have mentioned templates in following way:
# Build paths inside the project like this: join(BASE_DIR, "directory")
BASE_DIR = dirname(dirname(dirname(__file__)))
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATICFILES_DIRS = [join(BASE_DIR, 'static')]
MEDIA_ROOT = join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
# Use Django templates using the new Django 1.8 TEMPLATES settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
join(BASE_DIR, 'templates'),
# insert more TEMPLATE_DIRS here
join(PROJECT_PATH, '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',
],
},
},
]
I am new to Django and python and this is my first app in django
create following structure:
buddy (this is your existing budy app folder)/templates (this is existing folder)/buddy (this is new folder) and place all your templates: addprofile.html, edit_profile.html, show_profile.html, success.html withing this path.
edit your settings 'DIRS' so it looks as follows:
'DIRS': [
join(BASE_DIR, 'templates'),
join(PROJECT_PATH, 'templates/'),
join(BASE_DIR, 'buddy/templates')