I tried uploading staticfiles:
aws:elasticbeanstalk:enviroment:proxy:staticfiles:
/static: /static
got this error in
2022-04-27 03:34:07 ERROR "option_settings" in one of the configuration files failed validation. More details to follow.
2022-04-27 03:34:07 ERROR Invalid option specification (Namespace: 'aws:elasticbeanstalk:enviroment:proxy:staticfiles', OptionName: '/static'): Unknown configuration setting.
2022-04-27 03:34:07 ERROR Failed to deploy application.
ERROR: ServiceError - Failed to deploy application.
I also tried only doing
python manage.py collectstatic
and it did not work
I tried my settings.py in this way:
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
and this way(current way im utilizing):
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
STATICFILES_DIRS = [BASE_DIR / 'templates/static']
You can try following configuration which worked for me.
settings.py
DEBUG = False
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
Run python manage.py collect static
Go to your root urls.py and add
from django.conf.urls import url
from django.conf import settings
from django.views.static import serve
urlpatterns = [
...
...
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
you can refer Github
Related
i uploaded my Media directory which contains my Images but i cant access it to display
my images. My images are correctly being uploaded when i send them, but i cant view them in the page
Im utilizing ElasticBeanstalk and RDS - Mysql
I only configured my RDS everything else like EC2, S3 are automattically configured by ElasticBeanstalk
commands I used on eb:
eb init
eb create
eb deploy
static-files.config:
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
/media: media
django.config:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: store.wsgi:application
01_packages.config:
packages:
yum:
python3-devel: []
mariadb-devel: []
models.py:
class Product(models.Model):
image = models.ImageField(upload_to='product_images/%Y/%m/%d')
settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
my html:
<a href="{{product.get_absolute_url}}">
<img class="default-img" src="{{product.image.url}}}" alt="550x750">
</a>
You can try following configuration which worked for me.
settings.py
DEBUG = False
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
Now run python manage.py collectstatic
In root urls.py make following configuration
from django.conf.urls import url
from django.conf import settings
from django.views.static import serve
urlpatterns = [
...
...
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
You can refer Github for better idea. Thank you
I am trying to deploy a Django app on a Windows server. I am able to make the pages load and am using wgsi. I am also able to load pages with images when using runserver, just not when accessing via the webserver. I have DEBUG = False.
My settings.py looks like this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
STATIC_ROOT = "/assets/"
When I do collectstatic, my files are copied into the assets folder.
But, when served, I receive:
GET http://localhost:8000/static/js/main.js net::ERR_ABORTED 404 (Not Found)
Try this in your urls.py:
urlpatterns = [
.....
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT )
also define the media root and url in you settings.py:
MEDIA_ROOT = 'media'
MEDIA_URL = '/media/'
for more details: https://docs.djangoproject.com/en/2.1/howto/static-files/#serving-files-uploaded-by-a-user-during-development
I am trying to deploy my django app on python anywhere, but I believe media_urls or media_root may be set incorrectly. I also spent time trying to figure out how to print all valid urls on django, but nothing work for me on this link. Django : How can I see a list of urlpatterns?.
I received a "ModuleNotFoundError: No module named 'django.core.urlresolvers'"
from .base import *
DEBUG = False
ALLOWED_HOSTS = ["*"]
MEDIA_ROOT = os.path.join(BASE_DIR, "images")
MEDIA_URL = '/snapcapsule/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
Answer
Your static file mapping for images is at at /images, but you're accessing the image from /snapcapsule/images. Either change your media_url or your static file mapping so that they match.
Most of the time, I server static and media with django at local and with nginx in server.
At local:
# at the end of urls.py
if settings.DEBUG:
# debug toolbar
import debug_toolbar
urlpatterns.insert(0, path("__debug__/", include(debug_toolbar.urls)))
# static and media
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns.extend(
staticfiles_urlpatterns()
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
)
In server:
# nginx.conf
server {
...
location /static/ {
alias /path/to/project/static_collection/;
}
location /media/ {
alias /path/to/project/media/;
}
}
I have a Django 1.9.6 site deployed to Heroku. When DEBUG=False I was getting a server error (500). The logs contained no useful information, so I tried running it with DEBUG=True. Now it works fine. I think the issue may be tied to my scss file processing, which really confuses me and I was struggling with. I recently--among other things--added COMPRESS_OFFLINE = True to my settings files, and commenting that out seems to alleviate the problem (although then my scss files don't work).
Some of my static settings.py. Let me know if you need more--so much of this is a mystery to me. I was trying to follow this as best as I could.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# other finders..
'compressor.finders.CompressorFinder',
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
in urls.py:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
url(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
urlpatterns += staticfiles_urlpatterns()
EDIT:
I've gotten logging to work, and I've confirmed that it's a compress error. I'm getting the error message:
Internal Server Error: /
OfflineGenerationError at /
You have offline compression enabled but key "171c3b7763dbc51a465d996f7d920cf5" is missing from offline manifest. You may need to run "python manage.py compress".
which is the same thing I've gotten locally, except running the suggested command solved it. Running heroku run python manage.py compress doesn't have an effect (no errors running it, though)
The manifest generated by compress was stored in my .gitignore and therefore the one on production was stale. Adding it to the git repository fixed everything.
First off set value for ALLOW_HOSTS, this can't be blank when debug is off.
ALLOWED_HOSTS = ['.mydomain.com', '.2nddomain.com']
Because you use compress plugins:
SET
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
# this where the collectstatic and compress result output
# point your static alias to here
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# in your production env: activate ur virtual environment then run the compress statics command
python manage.py compress
python manage.py collectstatic
When Debug is off all exceptions is suppressed for security reason, set admin email in the setting file to let django email all un-caught exception
SERVER_EMAIL = 'ur#from-email-address.com'
ADMINS = (
('Exceptions Email', 'destination#email.com'),
)
Add this to your settings.py inside the loggers section and it should give you more information (this is what helped point me into solving the same problem).
"django.request": {
"handlers": ["console"],
"level": "ERROR",
"propagate": True
}
For what it's worth, here are my similar settings.py settings:
MEDIA_URL = "http://%s.s3.amazonaws.com/" % (AWS_STORAGE_BUCKET_NAME)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = os.getenv("DJANGO_STATIC_HOST", "") + "/static/"
if DEBUG:
STATIC_URL = "/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Note: I have no MEDIA_ROOT or STATICFILES_FINDERS and I'm also using Whitenoise with CloudFront for my static file handling
Today I tried to share a website with 'PythonAnywhere'. I have encountered the same problem and have fixed the problem with 'Allowed_Host'.
https://docs.djangoproject.com/en/1.10/ref/settings/#allowed-hosts
settings.py
ALLOWED_HOSTS = ['*']
I'm trying to deploy my Django application to the web, but I get the following error:
You're using the staticfiles app without having set the STATIC_ROOT
setting to a filesystem path
However, I did in my production.py:
from django.conf import settings
DEBUG = False
TEMPLATE_DEBUG = True
DATABASES = settings.DATABASES
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# Update database configuration with $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
What is the production.py file? How do you import your settings?
Depending on how you got this error (serving django through a wsgi server or on the command line), check for manage.py or wsgi.py to see what is the name of the default settings file.
If you want to manuallly set the settings to use, use something like this:
./manage.py --settings=production
Where production is any python module.
Moreover, your settings file should not import anything django related. If you want to split your settings for different environments, use something like this.
A file settings/base.py
# All settings common to all environments
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
Files like settings/local.py, settings/production.py…
# Production settings
from settings.base import *
DEBUG = False
DATABASES = …
If you are using Django 2.2 or greater, your settings file already has a line similar to this:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Therefore you can easily set static like so:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Set the STATIC_ROOT setting to the directory from which you’d like to serve static files, for example:
STATIC_ROOT = "/var/www/example.com/static/"
The settings you are using are for development. Check the Django docs for more information here
Django settings for static assets can be a bit difficult to configure and debug. However, if you just add the following settings to your settings.py, everything should work exactly as expected:
goto "settings.py" add following code
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
See a full version of our example settings.py on GitHub.
now create static folder in root directory, and a random file inside
it.
Django won’t automatically create the target directory (STATIC_ROOT) that collectstatic uses, if it isn’t available. You may need to create this directory in your codebase, so it will be available when collectstatic is run. Git does not support empty file directories, so you will have to create a file inside that directory as well.
for more refer: https://devcenter.heroku.com/articles/django-assets