when deployed server then static files not working and media files not diplaying. 404 error
here is the urls.py
from django.views.static import serve
import django
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path ('' , include('home.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings.py
STATIC_URL = 'static/'
STATIC_ROOT = '/usr/local/lsws/Example/html/demo/static'
"""STATICFILES_DIRS=(
BASE_DIR / "static",
)"""
MEDIA_URL = 'media/'
MEDIA_ROOT = '/usr/local/lsws/Example/html/demo/static/media'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CKEDITOR_UPLOAD_PATH = 'uploads'
Verify that on prod it's the same folder and the user running webserver has access to this folder. Consider using BASE_DIR instead of absolute paths. Also you might prefer to store projects files not inside user private home folder and store them somewhere /var/opt or whatever. If you had media files locally before deployment then they must be copied to prod manually. –
Thank you #Ivan-Starostin
Related
I have been trying to test my Django project before deploying it on a cpanel
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
====== in project urls.py ========
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
urlpatterns = [
path("", include("myapp.urls")),
path('admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns modification to serve static files is recommended in development.
If you want to serve your static files from the same server that’s already serving your site, the process may look something like:
Push your code up to the deployment server.
On the server, run collectstatic to copy all the static files into
STATIC_ROOT.
Configure your web server to serve the files in STATIC_ROOT under
the URL STATIC_URL. For example, here’s how to do this with Apache
and mod_wsgi.
How to use Django with Apache and mod_wsgi
I created static directory which included css and image files in my Django project. When I run my project the css do display("GET /static/website/barber.css HTTP/1.1" 200 69) but the images do not("GET /static/website/images/balloons.gif HTTP/1.1" 404 1840),
in your setting.py file define STATIC_URL
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
os.path.join(BASE_DIR,'boot'),
]
STATTC_URL = '/static/'
and add this to your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#your url
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Add STATIC settings
You need to create these definitions in settings.py to tell Django where is static files:
STATIC_DIR = os.path.join (BASE_DIR, "static")
STATIC_ROOT = os.path.join (BASE_DIR,'static files')
STATIC_URL ='/static/'# path to read css with local (probably)
STATICFILES_DIRS = [
os.path.join (BASE_DIR, "static"),
]
Be sure to import settings and static in urls.py.
You also need to add them to your main urls.py to access the URLs of images:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#Your urls
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have excel files in my static folder that I read from to get some data but the files arent there when I deploy my project
these are my settings for configuring static files
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
this is how I call them in my views
from django.conf import settings
from django.templatetags.static import static
from django.contrib.staticfiles.storage import staticfiles_storage
from django.contrib.staticfiles.finders import find
def get_static(path):
if settings.DEBUG:
return find(path)
else:
return static(path)
def sport_news(request):
path = 'excel\sport_update.xlsx'
excel_data = excel(get_static(path))
return render(request, 'news/news.html', {"excel_data":excel_data, 'type':'Sport News'})
please check in you settings.py
but first
pip install whitenoise
in middleware
'whitenoise.middleware.WhiteNoiseMiddleware',
in bottom
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
then your main projects urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and please make sure you do have a static cdn folder in your settings.py
then run python manage.py collectstatic
if you done all the thing mentioned above and still file is not please make sure that file is in the static cdn folder and if not push the code again after completing all the step and you will be good to go
if things still did not work for you tell me
Ok, I have a Django 1.10 project. The relevant settings look like this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = BASE_DIR + "/media/"
MEDIA_URL = '/media/'
I'm working locally, I can upload images correctly. But when I try to access the image on a template using {{ image.image.url }}, I get a 404. In the terminal I can see this:
[06/Sep/2016 18:13:43] "GET /media/folder/uploaded_image.jpg HTTP/1.1" 404 4900
But if I look into my folder, the file is there, correctly uploaded by django.
Try using os.path.join, like this:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
You probably also need to update your urls.py with this:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am using Django with runserver for my development. When I deploy to my production server I can see all my static files, but not on my local computer.
I did collectstatic and I have set DEBUG = True.
I found many different opinions online, the most prominent being the STATICFILES_DIRS, but that does not work for me.
How can I set it so that in my development environment I can see the static files, and when I upload my files to the server I do not need to make any changes for the production environment to work properly.
Edit - my urls.py file:
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
import newsflashes
import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('newsflashes.urls')),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Edit - file structure:
I have two directories, static and dynamic. Inside static are the static files, and in the dynamic directory are the django apps.
Edit - Settings:
My relevant settings are as follows
STATIC_ROOT = os.path.join(BASE_DIR, '..', 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
I managed to fix it.
I created another directory called static in my project folder called static, and copied there my static files.
I then added:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
to my urls.py
and
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
to my settings.py.
Then, when I deploy I execute manage.py collectstatic and since Apache is configured properly, everything will work!
Based on http://dlo.me/archives/2013/01/14/how-to-serve-static-files-django/
Thanks to all.
I usually run python manage.py runserver --insecure to force serving of static files with the staticfiles app even if the DEBUG setting is False.
Here's the link of their documentation. https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/#cmdoption-runserver-insecure