Django local development static files 404 [duplicate] - django

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

Related

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

Media Files and Static files not Displaying and working in Django

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

Django dev server STILL won't serve static files

Okay, all. I know this is a question that many people have solved in various cases, but I cannot for the life of me get my Django 1.9 development server to serve static content on my local computer. Static files worked fine pre-deployment, and are totally fine on my deployment server, but now in my test environment (local computer with runserver going) everything is broken, and I really need to be able to test stylesheets in a dev environment.
I have tried all of these solutions and more, followed the documentation guide, used collectstatic again in the development repo... nothing has worked, and I am at my wits' end.
Currently, I have DEBUG = True, and the following setup:
Folder Hierarchy
project/
manage.py
(&c)
app/
urls.py
models.py
(&c)
project/
settings.py
urls.py
(&c)
static/
styles/
images/
(&c)
settings.py Static Files Settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(STATIC_ROOT, 'styles/'),
os.path.join(STATIC_ROOT, 'js/'),
os.path.join(STATIC_ROOT, 'audio/'),
os.path.join(STATIC_ROOT, 'images/'),
os.path.join(STATIC_ROOT, 'admin/'),
os.path.join(STATIC_ROOT, 'documents/'),
)
urls.py URL Patterns
from django.conf.urls import include, url, patterns
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
# ... project url patterns blah blah ...
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATIC_ROOT, 'show_indexes': True}))
I also have {% load staticfiles %} in my templates along with the appropriate {% static %} calls (ex: {% static 'styles/main.css' %}).
For reference, the command line gives me the following when I load the page:
"GET /static/styles/main.css HTTP/1.1" 404 1759
If anyone knows of a fix I have missed that might even remotely have a snowball's chance of working, please let me know. It's driving me bonkers not being able to test properly.
EDIT: As suggested, I have updated to Django 1.11 on my local machine, with no changes to the current issue.
I'm not able to test it out, but maybe try changing this:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
to this:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
By the way, version 1.9 is unsupported and also a security risk. You'd probably want to upgrade to another version.
Can you try to create the STATIC_ROOT var with the absolute path and check if it works?
Maybe the construction of that var is not OK.
You could also check the STATIC_ROOT var through
python manage.py shell
and see if it correctly fits your path.

Static files status' 404

I'm using heroku to host my Django(1.6) app (called 'Zen'). The problem is static files aren't showing. In other words, there's no CSS and no JS in my app because it doesn't found those files. I looked to other questions here and I configured my app as below:
Settings.py:
##### Static asset configuration #####
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
urls.py:
from django.conf.urls.static import static
from zen import settings
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zen.settings")
application = Cling(get_wsgi_application())
Apparently, after pushing my app with git push heroku master all is working great:
-----> Preparing static assets
Running collectstatic...
69 static files copied to '/app/staticfiles'.
But as you can see there is no CSS in my app (http://obscure-reef-8874.herokuapp.com/). I looked to my app's log and mostly css and js files are in 404 status... I tried everything, can you help me?
EDIT:
I didn't find the bug, I recreated an app and it worked, simple.
Set
STATIC_ROOT = 'static'
try
May be caused by different STATIC_ROOT and STATICFILES_DIRS
The problem is not with your static file serving: that is fine, as you can see if you go to the admin application: http://obscure-reef-8874.herokuapp.com/admin/. Files under /static/admin/ are being served with no problem.
Without seeing the structure of your application and the way you are outputting the static links in your template it's hard to help, I guess that your files are inside a subdirectory of /static/, so you'll need to reference that subdirectory in the links to the assets in your template.
Note that you should remove the additional static URL patterns from your urls.py. They do not work when DEBUG is False. Your files are served by Cling, which is external to Django.

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