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/
Related
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!
Hope you are all having a very good Friday!
I am running a django project on nginx, and my project is each time going into this url - https://sitename.com/internal-nginx-static-location_uat/project_name/static/images/success.svg
I didn't configure this internal-nginx-static, and If I load the same project on another website, it works fine, is it something I can handle in code, or I have make changes in server conf.
Here is my URL.py Files
from django.conf import settings
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
path('', include('portal.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(DATA_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
I am using Django 3, and I have tried every solution available on the internet, but none of them worked for me.
When I set DEBUG=False, I am unable to display an image on the HTML page.
Here are my settings
-root_app
--main_app
---settings.py
---asgi.py
---urls.py
---wsgi.py
--sub_app
---admin.py
---urls.py
---views.py
---static
----style.css
---templates
----home.html
---media
----images
media path in settings.py
MEDIA_ROOT = os.path.join(BASE_DIR,'sub_app','media')
MEDIA_URL = '/media/'
Here what I have done to resolve it
add context_processor
django.template.context_processors.media'
add +static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in url_patterns of urls.py of sub_app
I changed static storage to STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage', otherwise I got 500 error. Check this comment
But still, I am unable to display the image at home.html page.
When I click on image source using inspect element, I got this
/media/images/img.png
These settings resolved the issue
Media Path
MEDIA_ROOT = os.path.join(BASE_DIR, 'sub_app','media')
MEDIA_URL = '/media/'
Static Path
STATIC_ROOT = os.path.join(BASE_DIR, 'sub_app','static')
STATIC_URL = '/static/
Adding re_path line in urls.py of main_app
from django.conf.urls.static import static
from django.views.static import serve
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('sub_app.urls')),
re_path(r'^media/(?P<path>.*)$', serve, kwargs={'document_root': settings.MEDIA_ROOT})
]
Change this
MEDIA_ROOT = os.path.join(BASE_DIR,'sub_app','media')
to
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
in settings.py and use
{{MEDIA_URL}}images/image.png
inside your template.
An edit to Dr. Abhishek's answer: yes, you need the MEDIA_ROOT setting, but you need to append it into the STATICFILES_DIRS list also, which will look something like this:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = [
# ...
os.path.join(BASE_DIR, 'media'),
]
When you load the image, use a soft-encoded path like this:
{% load static %}
<!-- remember to change the file name/type! -->
<img src="{% static 'images/image.jpg' %}" alt="Image">
I'm new to Django and Python.I want to display a link to my files stored in my static folder.Here is the required code from settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_URL = "/media/"
When I access these files through my admin I get the following url displayed in my browser:
http://127.0.0.1:8000/media/filename.pdf
Hence what I tried is to give a link to the file in my HTML code :
File
But instead of displaying http://127.0.0.1:8000/media/filename.pdf it just displays /media/filename.pdf which results in an error.
Adding localhost:8000/ before {{instance.docFile.url}} also didn't work.
Why isn't this working?What is the correct way to display link to my files?
Let me know if anything else is required.Thank you.
in your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in your settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/uploads/'
Then in your template
File
If you want know refer the docs Manage static files
The error is coming because your file is not stored in media.it is stored in static but your database store media URL. change your MEDIA_ROOT (given below) and try to upload one more time
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Don't put slash
:
File
Please see the Django setting MEDIA_ROOT documentation to better understand what your trying to do.
First MEDIA_ROOT and STATIC_ROOT must have different values.
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
See static files documentation for more details.
Second, You need to add these settings to your base urls.py.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If you want to use {{ MEDIA_URL }} in your templates, add 'django.template.context_processors.media' in the 'context_processors' option of TEMPLATES in settings.py.
At the top of your HTML page add {% load static %} then to display your media:
<img src="{{ MEDIA_URL }}{{ docFile }}" alt="test">
Please take your time with the Django Documentation, trust me it will help and also checkout the File upload settings
I am learning Django, and am trying to load a static css file.
I have seen the other questions and read the docs, but I am still unable to see the problem.
I am using Django 1.11.
Here is my urls.py:
from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
url(r'^$', views.index),
url(r'^admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My settings.py (only the part to do with static files):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = (
STATIC_ROOT,
)
And the part of my template where I try and load the files:
{% load static %}
Whenever I load the index.html template, the following error messages are displayed by my console:
core.js:5 Uncaught ReferenceError: define is not defined
at core.js:5
localhost/:12 GET http://localhost:8000/static/css/homepage.css
localhost/:11 GET http://localhost:8000/static/css/horz-navbar.css
localhost/:10 GET http://localhost:8000/static/css/fonts.css
localhost/:13 GET http://localhost:8000/static/css/style.css
Here is the file directory structure:
mysite
db.sqlite3
manage.py
mysite
__init__.py
settings.py
urls.py
views.py
wsgi.py
static
admin
css
fonts.css
horz-navbar.css
homepage.css
style.css
templates
index.html
So Django doesn't seem to recognize that the files exist, I have checked and made sure that the files exist on my computer, and I'm not sure if this was needed, but I have also run python manage.py collectstatic
Please tell me if you need anymore information.
Change your STATIC_ROOT into another name and then update your STATICFILES_DIR. Something like this:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Replace "{% static "css/fonts.css" %}" with "{% static 'css/fonts.css' %}". There's a mismatch between quotations.