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
Related
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
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 have an issue with django. I recently bought an instance of a shared server and I wanted to move my django website from AWS to this server (which use Cpanel). All worked fine with AWS but when I switched to Cpanel all statics files were missing.
this is my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
my project structure:
my_project
|-app/
|-...
|-views.py
|-db.sqlite3
|-manage.py
|-media/
|-my_project/
|-...
|-settings.py
|-static/
|-main_page/
|-js/
|-my-script.js
I add static files like this:
{% load static %}
<script src="{% static 'main_page/js/my-script.js' %}"></script>
This is the error:
GET http://my.domain.com/static/main_page/js/my-script.js net::ERR_ABORTED 404 (Not Found)
When I go to the URL of the file it understands it like one of my URLs:
I hope you will help me to solve this issue ;)
thanks.
you need to add the static & media files config in the urls.py , like this
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the django docs : https://docs.djangoproject.com/en/3.1/howto/static-files/
I'm using Amazon S3 to be my static and media files storage. My Django project is running in Digital ocean ubuntu 16.04.
After running python manage.py collectstatic I found the CSS and js did not work in my website. And then I found that the CSS and js hadn't been upload in the S3. I think this is the reason why the cs and js did not work because they are not in there.
there is only 'static' folder in S3.
in this static folder there are not my project static files but the admin xadmin and another plug's static files
Above is the folder under static in S3.
When I check the url of the js it looks like this:
<link rel="stylesheet" href="https://myproject.s3.amazonaws.com/css/main.css?Signature=imJphDmnb4U%2BWOWHjE0Iagk2tow%3D&AWSAccessKeyId=AKIAI4LFEI2ASSMOYRTQ&Expires=1537337559">
<link rel="icon" href="https://myproject.s3.amazonaws.com/images/logo-blue.png?Signature=ACidpeC946mBazTtHx0McVIk6rM%3D&AWSAccessKeyId=AKIAI4LFEI2ASSMOYRTQ&Expires=1537337559">
But in my project the main.css is under static folder, the images are under media folder. This is so weird for me.
This is my project folder looks like:
And I found that, after running python manage.py collectstatic the system didn't copy the static and media at all
Here is the main part of my settings.py:
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
ROOT_URLCONF = 'myproject.urls'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
AWS_ACCESS_KEY_ID = 'myproject'
AWS_SECRET_ACCESS_KEY = 'myproject'
AWS_STORAGE_BUCKET_NAME = 'myproject'
AWS_S3_FILE_OVERWRITE = False
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
# the sub-directories of media and static files
STATICFILES_LOCATION = 'static'
MEDIAFILES_LOCATION = 'media'
# a custom storage file, so we can easily put static and media in one bucket
STATICFILES_STORAGE = 'myproject.custom_storages.StaticStorage'
DEFAULT_FILE_STORAGE = 'myproject.custom_storages.MediaStorage'
# the regular Django file settings but with the custom S3 URLs
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
I also created a file named custom_storages.py:
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage
class StaticStorage(S3Boto3Storage):
location = settings.STATICFILES_LOCATION
class MediaStorage(S3Boto3Storage):
location = settings.MEDIAFILES_LOCATION
I followed this tutorial https://blog.bitlabstudio.com/ultra-short-guide-to-django-and-amazon-s3-2c5aae805ce4
This issue is really very complicated to me. Any friend can help? This issue spends me 2 days to solve it.
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/;
}
}