settings.py
STATICFILE_DIRS = [os.path.join(
BASE_DIR, 'build/static')]
STATICFILES_STORGE =
"whitenoise.storage.CompressedManifestStaticFilesStorage"
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media/images")
WHITENOISE_ROOT = os.path.join(BASE_DIR, "media")
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
# from whitenoise.django import DjangoWhiteNoise
from whitenoise import WhiteNoise
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dj_gmc.settings')
application = get_wsgi_application()
# application = DjangoWhiteNoise(application)
application = WhiteNoise(application, root='build/static')
application.add_files('build', prefix='more-files/')
application.add_files('media', prefix='more-files/')
can't find a image source it's work in normal localhost prefect
but in deploy all images gone until the frontend image
The problem has been solved after many searches and this solution if someone is interested
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'build/static'),
os.path.join(BASE_DIR, 'build/media'),
os.path.join(BASE_DIR, 'media/images')
)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
importent urls.py
from django.views.static import serve
re_path(r'^static/(?P<path>.*)$', serve,
{'document_root': settings.STATIC_ROOT}),
re_path(r'^media/(?P<path>.*)$', serve, {
"document_root": settings.MEDIA_ROOT
})
Related
I was trying to save an image but it can't be displayed through the browser. It was successfully saved in the exact location but when I pasted the link it says, "Page not found". Could someone help me with what's going on?
See output:
Browser View
Code:
Code Snippet
Settings configuration:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_LOCATION = "static"
MEDIA_LOCATION = "media"
settings.py
from pathlib import Path
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
This must be applied in your settings configuration.
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
....
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
By using gunicorn without nginx from digitalcloud tutorial
my server is runing and on the console is
not found: /static/style.css
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
I already tried to
collectstatic
do urlpatterns += staticfiles_urlpatterns() in urls.py file
makemigrations + migrate
maybe django try read 'static//static/style.css' but you need this: 'style.css' ;) ?
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
urlpatterns = [your urls here] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You can create in your project folder settings then in them create settings files like names:
local.py
from my_project.settings import *
DEBUG = True
ALLOWED_HOSTS = []
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this run in VM like: python manage.py runserver --settings settings.local
remember in urls add:
if settings.DEBUG == True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
web.py
from my_project.settings import *
DEBUG = False
ALLOWED_HOSTS = ["www.side.com", "side.com"]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and then in gunicorn import only web.py file project to read in nginx
this run in VPS server.
In the result (real server) the css, js and images are not connected, but the thing is that in localhost it works perfect. I dont know what any other details do you need so write comment and I will edit this queston :)
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_ROOT = os.path.dirname(__file__)
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [STATIC_DIR]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CRISPY_TEMPLATE_PACK = 'uni_form'
LOGIN_URL = ''
Most probably you misconfigured nginx file or did not mention in nginx file.
You need to write path in nginx file something like below:
location static_url {
alias static_dir
}
I have modified my Geonode project, which is a GeoDjango project, to enable multi-tenancy using django-tenants. I am currently not able to view my thumbnails due to broken routing...
How do I correctly route my generated thumbnails like this: http://d3.demo.com(current_tenant_domain_url)/uploaded/d3(tenant)/thumbs/document-8a72dc8c-0151-11eb-a488-1062e5032d68-thumb.png
The thubnail url that is currently generated is as follows: http://localhost:8000/uploaded/thumbs/document-fcdea3a4-015c-11eb-a488-1062e5032d68-thumb.png?v=c1855f6a
urls.py
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.LOCAL_MEDIA_URL,
document_root=settings.MEDIA_ROOT)
My current settings.py
MEDIA_ROOT = os.getenv('MEDIA_ROOT', os.path.join(PROJECT_ROOT, MEDIAFILES_LOCATION))
MEDIA_URL = os.getenv('MEDIA_URL', '%s/%s/%s/' % (FORCE_SCRIPT_NAME, MEDIAFILES_LOCATION, MULTITENANT_RELATIVE_MEDIA_ROOT))
Any help will be appreciated
settings.py
MIDDLEWARE = [
'django_tenants.middleware.main.TenantMainMiddleware'
]
DATABASE_ROUTERS = (
'django_tenants.routers.TenantSyncRouter',
)
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = 'django_tenants.files.storages.TenantFileSystemStorage'
MEDIA_ROOT = os.path.join(BASE_DIR, 'public', 'media')
urls.py
urlpatterns = [...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I've this in my urls.py
if settings.DEBUG:
handler404 = 'views.custom_404'
handler500 = 'views.custom_500'
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "static", "media")
STATIC_ROOT = os.path.join(BASE_DIR, "static", "static_root")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static", "static_files"),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
I've this in wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ecommerce.settings")
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
Eveything works fine when in development when DEBUG & TEMPLATE_DEBUG is set to True. Once set to FALSE, I can only see static files on the site.
For example this static loads
src="/static/img/placeholder.svg"
but this media doesn't
src="/media/foodtruck/logo/banner2.jpg"
The folders are set up as such
root\static\media\foodtruck\logo\banner2.jpg
root\static\static_root\img\placeholder.svg
in html
<img src="{{ MEDIA_URL }}{{ truck.logo }}">
<img src="{{ STATIC_URL }}img/placeholder.svg">
STATIC_ROOT is only used in a non-DEBUG environment, you should let your webserver figure out where you static files are in production and collect files accordingly using the collectstatic management command, here's an apache example
Alias /static/ /var/www/example.com/static/
<Directory /var/www/example.com/static>
Options -Indexes
Order Deny,Allow
</Directory>
n.b. there's no need to use the prefixes in templates using the static tag
{% static "img/foo.jpg" %}