Django test server gives 404 to static files - python

In production, I'm using NGINX and set an alias for static files, so this is fine.
However, in test server mode, I get 404 for all of the static files. They are located in the base of the project and here are my relevant settings:
DEBUG = True
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
And here is my urls.py
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
urlpatterns = patterns(
'',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'home.views.home', name='home'),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
When running the test server, I try to get http://127.0.0.1:8000/static/home/img/logo.png, which is located in $BASE_DIR/static/home/img/logo.png, but get 404.
'home/img/logo.png' could not be found
and in the test server log
[03/Feb/2015 17:49:08] "GET /static/home/img/logo.png HTTP/1.1" 404 1655
Any ideas?

STATIC_DIRS should be STATICFILES_DIRS

Related

Django Media Files Not Showing

My Django files are loading after upload and are shown in the media folder, however I cannot access them at localhost:<PORT>/media/<FILENAME._EXT>. I've looked at several other answers on stackoverflow and they haven't worked. For example adding the urlpatterns += static(...), having DEBUG=True in settings.py.
When accessing: http://localhost:8000/media/controller.gif:
Error:
lightchan-backend-1 | Not Found: /media/controller.gif
lightchan-backend-1 | 2022-03-06 16:37:34,875 WARNING Not Found: /media/controller.gif
In settings.py:
DEBUG = True
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
In my urls.py:
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('comment/<int:comment_id>/', views.comment, name='comment'),
path('comments/', views.comments, name='comments'),
path('reply/<int:incoming_id>/', views.reply, name='reply')
]
# if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django 3.1 not serving media files correctly

I have a project running in Django 3.1, and suddenly it has started to fail serving media files (static files uploaded by users), even though I haven't changed anything in settings.py or elsewhere.
My main urls.py:
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('cart/', include('cart.urls', namespace='cart')),
path('', include('contacts.urls', namespace='contacts')),
path('customers/', include('customers.urls', namespace='customers')),
path('orders/', include('orders.urls', namespace='orders')),
path('account/', include('account.urls')),
path('', include('catalog.urls', namespace='catalog')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
From my settings.py:
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath( os.path.join(__file__, os.pardir))))
DEBUG = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
I don't know and can't figure out where the mistake is, but runserver just keeps throwing 404 when trying to load media files, even though static files (CSS/JS) are being served correctly.
your Urls looks fine, although i notes that you missing from django.contrib import admin.
About your settings, here is mine that works just fine for me.
Add import os to your settings.py if it is not there.

Only Django Admin pages visible while rest of Site blank in production

Im trying to host my site on heroku on my localhost my project is fine with no issues , on deploying it all I get are blank pages (I inspected it and elements are shown but nothing is visible on webpage)apart from admin pages I installed all the necessary packages as I was deploying that is to say gunicorn and whitenoise as well as setting STATIC_ROOT and STATICFILES_STORAGE . I at first set DISABLE_COLLECTSTATIC = 1 and later set it to 0 when deploying, I have failed to see any error that might be causing the issue
some of my files for context
settings
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_dev')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
urls
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.conf.urls import handler404
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('user.urls')),
path('users/', include('django.contrib.auth.urls')),
path('accounts/', include('allauth.urls')),
path('', include('pages.urls')),
path('store/', include('store.urls')),
#path("djangorave/", include("djangorave.urls", namespace="djangorave")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#error handlers
handler404 = 'store.views.error_404'
#handler500 = 'store.views.my_custom_error_view'

Django Upgrade: Static Files Not Served

I have upgraded to Django 1.10 from 1.9.6. Below is my urls.py file which previously worked:
from django.conf.urls import include, url
from django.contrib import admin
from django.views.static import serve
from dwad import settings
urlpatterns = [
url(r'', include('meta.urls')),
url(r'^straightred/', include('straightred.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^chaining/', include('smart_selects.urls')),
url(r'^tinymce/', include('tinymce.urls')),
url(r'^accounts/', include('allauth.urls')),
]
# Get Django to serve media files in debug mode.
if settings.DEBUG:
urlpatterns += [url(r'^resources/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})]
if not settings.DEBUG:
urlpatterns += [
url(r'^resources/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
]
When try to run the site I get:
view must be a callable or a list/tuple in the case of include().
I know the above error is due to the 'django.views.static.serve' being in strings. If I remove them from strings and link to the actual view i get the following error:
name 'django' is not defined
If I delete everything from "Get Django to serve media files in debug mode." and below the site loads but no static or media files are served. This obviously means no css is applied and no images are loaded.
If people can offer some advice on next steps that would be appreciated.
Some settings that may be useful:
# Static files
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/str8red.com/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
MEDIA_URL = '/resources/'
MEDIA_ROOT = 'media' if DEBUG else '/var/www/str8red.com/resources/'
The reason behind the error is that, Django 1.10 no longer allows you to specify views as a string (e.g. 'myapp.views.home') in your URL patterns.
Try this,
from django.conf.urls.static import static
if not settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
#If you are in production (means running using nginx, or apache),
# you don't need this setting. Because, the media and static files
# are served by the nginx/apache, instead of Django.
#if settings.DEBUG:
#urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

Django file-browser and error 404

I'm using django admin site (v1.9.6) and recently installed file-browser for managing images and documents. I followed installation steps and file-browser is shown in admin interface but I cant upload images and documents. When trying to access them 404 errors shows up (also thumbnails are not shown in file-browser page):
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/uploads/aaaaaaaaaaalula.png
But the file is there, if I make
ls media/uploads/aaaaaaaaaaalula.png
it shows the file, so it's uploaded.
my settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/admin/'
and url.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from filebrowser.sites import site
urlpatterns = [
url(r'^admin/filebrowser', include(site.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include(admin.site.urls)),
]
Thanks in advance for help.
C.
Ok, here is the solution:
https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development
The issue was that I was testing django-filebrowser with development server
You are not putting url for displaying image.
In setting.py-
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '/media/'
In URLs.py--
Url(r'^media/(?^<path>.*)$','django.views.static.serve',{'document_root':
settings.MEDIA_ROOT,}),
I think it can help you
You can do:
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
#your urls
]
if settings.DEBUG: # will be 'True' in development
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Categories

Resources