Django static images receive 404 error in windows production - python

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

Related

AWS ElasticBeanstalk how to upload staticfiles with django

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

AttributeError at /media/.. when requesting file in media folder Django development stage

When I am reqeuesting media files (.png in this case) in the media folder in development I get this error:
AttributeError at /media/test.png
This FileResponse instance has no content attribute. Use streaming_content instead.
Request: http://localhost:8000/media/test.png
I added below to the url patterns:
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And below to the settings:
STATIC_ROOT = BASE_DIR / 'static'
STATIC_URL = '/static/'
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
Static files are working just fine.
What do I overlook?
Thanks!
hello if you have debug set to True you have to use this:
first go to you settings and set all you static file directories in
STATICFILES_DIRS = [
list of your static file,.......,]
the second step you should have this in your urls.py
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATICFILES_DIRS[0])
note:there is a difference between static_root and staticfile_dirs
static_root is only used for production
this is my mail for more explanation :nguewofoss#gmail.com
I found the problem.
In the newer django version staticfiles are rendered with a streamingHttpResponse, this doesn't have a content attribute.
I found below code in my code that points to the (non existing) content attribute:
if b'<html' in response.content[:100].lower():
I added this to resolve the problem:
if not hasattr(response, 'content'):
return response
if b'<html' in response.content[:100].lower():
Thanks all!

404 Static file not found - Django

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/

In django, what is correct setup for media deployment on python anywhere

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/;
}
}

Django Static Root 404 Error

Right now I am trying to connect my django project html files to static files but keep getting 404 errors. My Project is organized as
src
admin
homePage
media
static
homepage
css
images
fonts
js
templates
in my settings file i have
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'myProjectName/static'),
)
and in my html file
{% static "home/css/bootstrap.css"%}
Why do I keep getting 404 errors ? Thank you for your time.
Turn on Debug=True in your settings file and the error page will help you to find out the issue.

Categories