django and heroku: serving static files locally bot not on heroku - django

This is my static files configuration:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
my DEBUG is True and my urls.py don't mention static at all:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^', admin.site.urls),
url(r'^chats/', include('chats.urls')),
]
so locally I can get my static files served perfectly:
curl http://127.0.0.1:8000/static/dummy.css
.test_div {
background-color: red;
}
curl http://127.0.0.1:8000/static/admin/css/base.css
.... an admin CSS file gets served here ....
however, when I deploy the same exact configuration on Heroku - I get 404 errors for admin files and for my custom static files:
curl https://pleshka-timer.herokuapp.com/static/admin/css/base.css
... a 404 message....
The heroku collectstatic command that it runs with every code deployment finishes without errors. If I explore the dyno with heroku run bash I see that my staticfiles directory is sitting there, waiting to be served.
What am I missing?
By the way, I can always do this
urlpatterns = [
url(r'^', admin.site.urls),
url(r'^chats/', include('chats.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
as per Django docs, and this would work; but why the hell should I do this if Heroku does the collectstatic routine for me?

Related

All static files returning 404 error when deploying Django Project using Digital Ocean

I have tested my app in development and successfully got it fully functional in development running on my local server. I have now been trying to push it into production and none of the static files are being served. The connection has been successful as the domain shows the app with just plain HTML. However, all static files are returning the same 404 error.
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))`
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
EDIT: installing whitenoise fixed this!
try this :
urls.py
from django.conf.urls.static import static
urlpatterns = [
...
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Or just try this command:
python manage.py collectstatic
try this on top every HTML file if you didn't:
{% load static %}
this is because when you add some static files then you must tell manually the template to load those static file.
for more you can see here

Media files showing 404 debug is false using Django development server

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

django static files when hosting in heroku

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

404 - File not found error for static files while deploying Django and Vue js integrated project in Azure web services

I created a project with Django as backend and Vue js as frontend. Initially I ran Django on one server(8000) and Vue js on another (8080) with node.js . Then, I integrated Both to the same server with proxy changes in vue.config.js. Then I ran the command NPM RUN BUILD on terminal and I moved the dist folder to Django's static folder to render. This worked perfectly in local development environment but I'm getting 404 error for static files while deploying the project in Azure web services through git-hub. Thanks for any fix or sugesstions.
Here is my settings.py file. I have also tried commenting STATIC_ROOT
STATIC_URL = '/static/'
# Vue assets directory (assetsDir)
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Here is my vue.config.js
module.exports = {
devServer: {
proxy: {
'^/api/': {
target: 'http://127.0.0.1:8000/api/',
ws: false,
}
}
},
// outputDir must be added to Django's TEMPLATE_DIRS
outputDir: './dist/',
// assetsDir must match Django's STATIC_URL
assetsDir: 'static',
publicPath: '',
baseUrl: '',
}
Here is my project file structure
Here is the error in my console
If you are running on production server, static files are served differently from dev server. Additional changes has to be made in main urls and settings. Docs link
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# your url patterns
]
# for serving static files
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# for serving media files (files that were uploaded through your project application)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Obviously, you have to add STATIC_URL, STATIC_ROOT, MEDIA_URL, MEDIA_ROOT in project settings.py file.
Also, you need to run python manage.py collectstatic after setting STATIC_ROOT. Docs link 1 and link 2

Django runserver not serving static files in development

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