Static files not served in Django "dockerized" (dev environnement) - django

EDIT
- my_project
|_app
|_core
|_wsqi.py
|_settings
|_base.py <-- settings.py used for development
|_dev.py
|_prod.py
|_urls.py <-- modifications
|_requirements
|_base.txt
|_dev.txt
|_prod.txt
|_Dockerfile
|_Dockerfile.prod
|_entrypoint.sh
|_entrypoint.prod.sh
|_manage.py
|_.dockerignore
|_nginx
|_.env.dev
|_.env.prod
|_.gitignore
|_docker-compose.yml
|_docker-compose.prod.yml
# from django.conf import settings
import core
from django.conf.urls.static import static
urlpatterns = [
path('', views.home, name='home'),
path('registration/', include('django.contrib.auth.urls')),
path('randomization_management/', include('randomization_management.urls')),
path('randomization_settings/', include('randomization_settings.urls')),
path('randomization/', include('randomization.urls')),
path('export/', include('export.urls')),
path('contact/', views.contact, name='contact'),
path('admin/', admin.site.urls),
] + static(core.settings.dev.STATIC_URL, document_root=core.settings.dev.STATIC_ROOT)
I try to "dockerize" a Django apps.
All works fine except static files that are not served.
I use Django web server (dev) so I should not have to "served" static files.
Neverthelsess, I run docker exec -it coverage_africa_web_1 python manage.py collectstatic command
and get the confirmation
You have requested to collect static files at the destination
location as specified in your settings:
/usr/src/app/static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Found another file with the destination path 'randomization/js/script.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
140 static files copied to '/usr/src/app/static'.
settings.base.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'randomization_management/static'),
os.path.join(BASE_DIR,'randomization_settings/static'),
os.path.join(BASE_DIR,'randomization/static'),
)
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
.env.dev
SECRET_KEY=*************************************
DEBUG=1
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=db_dev
SQL_USER=user
SQL_PASSWORD=user
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
DJANGO_SETTINGS_MODULE=core.settings.dev
docker-compose.yml
version: '3.7'
services:
web:
build: ./app
restart: always
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./app/:/usr/src/app
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
restart: always
volumes:
- postgres_data:/var/lib/postgres/data/
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev
volumes:
postgres_data:

I see that you don't use a web server to serve the static files, so make sure that Django is serving the static files.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
have a look to to Django documentation https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development

OK, missing settings
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, "static") <- removed in dev
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), <- added in dev
os.path.join(BASE_DIR,'randomization_management/static'),
os.path.join(BASE_DIR,'randomization_settings/static'),
os.path.join(BASE_DIR,'randomization/static'),
)

Related

Django Static Files does not deploy on Google Cloud Bucket

I am deploying an Django App, which runs on google server but it does not load the static files, so that App does not load the css and images.
I checked the bucket I created but there is no files in the bucket.
Here is settings.py
STATIC_URL = '/static/'
STATIC_ROOT = "static"
PRODUCTION = True
if PRODUCTION:
# Define static storage via django-storages[google]
GS_BUCKET_NAME = env("GS_BUCKET_NAME")
STATIC_URL = "/static/"
DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
GS_DEFAULT_ACL = "publicRead"
else:
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
Here is app.yaml file.
runtime: python38
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
Here is urls.py.
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
When I deploy the app, it was done successfully. but the static files does not deploy into the bucket.
As well I run this bellow command.
python manage.py collectstatic
I followed this tutorial.
Here is structure of static directory.

Can't import my own css file in TINYMCE_DEFAULT_CONFIG = { 'content_css':

As I said in the title I can't import my css file in TINYMCE_DEFAULT_CONFIG variable 'content_css'.
I'm using django-tinymce4-lite package and setting 'content_css' in my settings.py file
I've tried with the boostrap cdn like this:
'content_css': 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'
and it worked but if I do something like this:
"content_css": os.path.join(STATIC_URL, "css/style.css")
I have the following 404 error:
"GET /static/css/style.css HTTP/1.1" 404 1764
my css file is in a static folder located in the root directory of my project like this:
/static/css/style.css
and my Static conf is:
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
urls.py
urlpatterns = [
path("grappelli/", include("grappelli.urls")),
path("admin/", admin.site.urls),
path("admin/filebrowser/", site.urls),
path("tinymce/", include("tinymce.urls")),
path("page/", include("pages.urls")),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
It's been a whole day looking for a solution, even the smallest clue is welcome.
Thanks
EDIT:
I'm in DEBUG mode running 'python manage.py runserver'
Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django = "*"
black = "*"
psycopg2-binary = "*"
pillow = "*"
django-tinymce4-lite = "*"
django-filebrowser-no-grappelli = "*"
[requires]
python_version = "3.6"
Add STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), ) to your settings.
Also remove STATIC_ROOT for your development settings or change it to something that's outside the project directory, for example one level higher:
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'static'))
This will make django collect static files to a static directory next to your project directory (but it could be a different location altogether, where your webserver will fetch them directly when you don't run runserver)
Note I wrote a more thorough explanation of the various settings, especially when deploying to production here on SO and here in a blog post

Django Media Directory HTTP 404 for Uploaded Images

My project structure is:
- api/
- urls.py
...
- avatars/
- 16701016.jpg
- 16701019.jpg
...
- frontend/
- static/
- frontend/
- templates/
- urls.py
...
- website/
- settings.py
- urls.py
...
Part of the settings.py file:
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.abspath('../avatars/')
MEDIA_URL = '/avatars/'
The contents of /website/urls.py :
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
re_path(r'^.*', include('frontend.urls'))
]
Now I need to access the /avatars/*.jpg files via URL like http://127.0.0.1:8000/avatars/*.jpg
But It is not working (Just 404). What is the problem?
Change the MEDIA_ROOT value from:
MEDIA_ROOT = os.path.abspath('../avatars/')
to:
MEDIA_ROOT = os.path.abspath('avatars')
And also append the following code in urls.py:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

How to get webpack file in django?

I've made a webpack file(as MainPage.js) in output folder which I set up on webpack.config.js However, when I access to my main page it says there is no MainPage.js. What's wrong with it?? (I'm using Django on backend and React on frontend)
Hierarchy of my folder
Cebula4
- back
- settings.py
- manage.py
- front
- webpack-stats.json
- webpack.config.js
- static
- bundles
- MainPage.js (I checked that it was existed)
Settings.py
...
BASE_DIR = "C:\\Users\\1Sun\\Cebula4"
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
]
STATIC_ROOT=os.path.join(BASE_DIR,'staticfiles')
...
WEBPACK_LOADER = {
'DEFAULT' : {
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'front/webpack-stats.json'),
}
}
...

django and heroku: serving static files locally bot not on heroku

This is my static files configuration:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
my DEBUG is True and my urls.py don't mention static at all:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^', admin.site.urls),
url(r'^chats/', include('chats.urls')),
]
so locally I can get my static files served perfectly:
curl http://127.0.0.1:8000/static/dummy.css
.test_div {
background-color: red;
}
curl http://127.0.0.1:8000/static/admin/css/base.css
.... an admin CSS file gets served here ....
however, when I deploy the same exact configuration on Heroku - I get 404 errors for admin files and for my custom static files:
curl https://pleshka-timer.herokuapp.com/static/admin/css/base.css
... a 404 message....
The heroku collectstatic command that it runs with every code deployment finishes without errors. If I explore the dyno with heroku run bash I see that my staticfiles directory is sitting there, waiting to be served.
What am I missing?
By the way, I can always do this
urlpatterns = [
url(r'^', admin.site.urls),
url(r'^chats/', include('chats.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
as per Django docs, and this would work; but why the hell should I do this if Heroku does the collectstatic routine for me?