I am running GoogleAppEngine (GAE) 1.6.3 with Python 2.7 and Django 1.3 by having:
libraries:
- name: django
version: "1.3"
in my app.yaml. The following should serve the admin media files at url /static/admin:
- url: /static/admin
static_dir: django/contrib/admin/media
expiration: '0'
But I get 404s for such admin media (css, etc). Am I using the correct location for the Django admin's media file?
The best way to do this is to copy or symlink the media directory into your app directory in your local files, so it is uploaded with your app's files. Then your app.yaml can refer to the relative path in the app directory.
There is a $PYTHON_LIB variable substitution you can use in app.yaml paths, but it looks like Django is not under $PYTHON_LIB in the live version of the Python 2.7 runtime.
When adding this to app.yaml
handlers:
- url: /static/admin
static_dir: static/admin
expiration: '0'
I was able to serve the CSS files by:
Adding this to settings.py:
BASE_DIR = os.path.abspath(os.path.dirname(__file__)) + os.sep
STATIC_ROOT = BASE_DIR + 'static'
And then running
python manage.py collectstatic
The admin media files appear correctly locally as well as on appspot.com.
The last command copies the media files into the the static/ directory. So in fact does what Dan Sanderson suggested but in a more automated way.
I tried Philipp Keller's collectstatic, but I don't have that command available.
So, add this handler to app.yaml:
- url: /static/admin
static_dir: django/contrib/admin/static/admin
expiration: '0'
then, in settings.py, delete ADMIN_MEDIA_PREFIX (removed in django 1.4) and add:
STATIC_URL = '/static/'
and you have working css.
is possible static file referenced by variable $PYTHON_LIB on deploy ??
file app.yaml
application: hello
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.3"
handlers:
- url: /admin/media
static_dir: $PYTHON_LIB/lib/django_1_3/django/contrib/admin/media
builtins:
- django_wsgi: on
log local:
INFO 2012-04-03 02:06:19,200 dev_appserver.py:2884] "GET /admin/media/css/base.css HTTP/1.1" 200 -
INFO 2012-04-03 02:06:19,207 dev_appserver.py:2884] "GET /admin/media/css/dashboard.css HTTP/1.1" 200 -
INFO 2012-04-03 02:06:19,242 dev_appserver.py:2884] "GET /admin/media/img/admin/default-bg.gif HTTP/1.1" 200 -
log error deploy app:
2012-04-02 19:17:32.775 /admin/media/css/dashboard.css 404 6ms 0kb
[02/Apr/2012:19:17:32 -0700] "GET /admin/media/css/dashboard.css HTTP/1.1" 404
Static file referenced by handler not found:$PYTHON_LIB/lib/django_1_3/django/contrib/admin/media/css/dashboard.css
Following seems to work fine for me.
app.yaml
handlers:
- url: /static
static_dir: staticfiles
settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Run python manage.py collectstatic. Now under your staticfiles admin folder should be created.
Related
Context
Hi, we have a big project in Django. Up until now, we have used Vagrant to run it. Now, we have decided to move to a container and use docker compose. Everything has worked out fine except for the serving of static files. We have a directory called web where we have all our static assets. Inside there, there are more subdirectories with more static assets.
Problem
Right now static files are not being served. Is curious because only some files are being served. I think the ones from the web directory are not working for some reason.
Current django config
This config has worked well before moving to docker.
# this is necessary because our settings file are stored inside a directoy
PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
PUBLIC_DIR = os.path.join(PROJECT_DIR, 'public')
STATIC_ROOT = os.path.join(PUBLIC_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'web'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Additional information
We are using django-1.11 and python 3.6.9. I know, it's not the latest.
Compose configuration
version: '3'
services:
web:
image: "dev"
container_name: web
ports:
- "8000:8000"
volumes:
- ./:/app
depends_on:
- db
networks:
- djangonetwork
db:
image: "postgres"
container_name: db
ports:
- "5432:5432"
networks:
- djangonetwork
networks:
djangonetwork:
driver: bridge
Any idea about what could be the problem?
I recently deployed a dockerized Django app on DigitalOcean app platform alongside a react static site using the app spec file. Because of the static site, I opted to serve Django on a non-root URL (something like https://example.com/api) and serve the react app on the root URL (https://example.com).
If I visit the Django URL, Django comes up but throws a "path not found error". None of the routes on my Django app works. The solution doesn't seem to be on google either and I don't know why.
Here is what my app.yaml looks like:
name: test
region: nyc
services:
- name: backend-api
github:
branch: deployment_app_platform
repo: NdibeRaymond/test
deploy_on_push: true
http_port: 8000
instance_count: 1
instance_size_slug: basic-xxs
source_dir: backend/
dockerfile_path: backend/compose/django/Dockerfile
routes:
- path: /api
static_sites:
- name: frontend
environment_slug: node-js
github:
branch: deployment_app_platform
repo: NdibeRaymond/test
deploy_on_push: true
source_dir: frontend/test/
build_command: npm run build
routes:
- path: /
gunicorn configuration
exec /usr/local/bin/gunicorn test.wsgi --threads=3 --bind 0.0.0.0:8000 --chdir /backend/test
root URL file
urlpatterns = [
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('summernote/', include('django_summernote.urls')),
path('', include('APIS.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I also tried
urlpatterns = [
path('api/admin/', admin.site.urls),
path('api/api-auth/', include('rest_framework.urls')),
path('api/summernote/', include('django_summernote.urls')),
path('api/', include('APIS.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Can you spot what I'm missing?
This is how my Django admin page looks like:
Though in my network section this is the result:
And the url path of these requests are correctly set by the static url, these are the Django settings:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
I collect statics before serving my app:
RUN python3 manage.py collectstatic --noinput (This is part of my Dockerfile)
some parts of my docker-compose:
web:
restart: always
image: <some private image>
ports:
- "127.0.0.1:8000:8000"
volumes:
- static_volume:/<path>/static
nginx:
container_name: nginx
image: nginx:1.19.3
restart: always
ports:
- 80:80
- 443:443
volumes:
- static_volume:/home/status/statics
volumes:
static_volume:
This is inside my nginx container:
Also this is in my https config for my nginx:
Well to me it seems like everything is working and I can't understand what's wrong with it.
I checked the console of the browser and I found some warnings. The styles were fetched as text and the problem was the wrong Content-Type header. I add mime.types file to my container and include that in my nginx config.
include /etc/nginx/mime.types;
I am using Google Cloud Platform's PHP based static server. My public folder contains one landing page and and a web-app.
The landing page just is a static html file with few css and image files which resides on root.
The web-app is made of React, it's index.html resides inside the folder named app
I have configured my react app to use BrowserRouter.
The web-app returns 404 error when I refresh any page inside the app.
How to configure app.yaml to solve this. My current configuration is below:
runtime: php55
api_version: 1
threadsafe: true
skip_files:
- src/
- node_modules/
- ^(.*/)?app\.yaml
- ^(.*/)?gitlab\.yml
- ^(.*/)?app\.yaml
- ^package\.json
- ^package-lock\.json
- ^README\.md
- ^webpack.config\.js
- ^(.*/)?#.*#
- ^(.*/)?.*~
- ^(.*/)?.*\.py[co]
- ^(.*/)?.*/RCS/.*
- ^(.*/)?\..*
handlers:
- url: /
static_files: public/index.html
upload: public/index.html
secure: always
- url: /app/(.*\.(html|js|css))$
static_files: public/app/\1
upload: public/app/.*\.(html|js|css)$
secure: always
- url: /(.*)
static_files: public/\1
upload: public/(.*)
secure: always
I want to achieve something like nginx server's try_files option.
I was wondering how you can configure your app to restrict certain endpoints to logged in users or even admin users.
Here is my app.yaml
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
- url: /admin/.*
script: main.app
login: admin
However this doesn't work. When visiting any endpoint beginning with /admin/ I can simply access the url without logging in. Is there any configuration setting that I'm missing?