Django static files urls setting - python

Please help me check my static setting
when Debug =False,it works well
But when Debug=True,It can't catch the static files
Please help me, Thank you.
Here is my settings.py:
DEBUG = False
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
urls.py
from django.conf import settings
urlpatterns = patterns('',
url(r'', include('core.urls', namespace='core')),
)
if settings.DEBUG is False:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
else:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

If you will write the below code in urls.py it will work for both the DEBUG status
url( r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT } ),
and one thing to be noted my static root is like for django 1.4+
PROJECT_DIR = os.path.dirname(os.path.dirname( __file__ ))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
along with this you need to do the following command to copy all static files to PROJECT_PATH/static dir
python manage.py collectstatic

Related

Why 404 error occurs even though I have set STATIC_ROOT and urls.py

I don't know why, but I get 404 errors when trying to use static file like this: {% static 'js/some.js' %}.
Here's my urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path("account/", include("account.urls")),
path("", include("post.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
And this is my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static/'
# media config
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / 'media/'
Thanks!!
EDIT
Here's the error I get
GET http://127.0.0.1:8000/static/js/some.js net::ERR_ABORTED 404 (Not Found)
And the url you can see here is right.
Please try this:
# settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]

Django is not serving static and media files in development but it is serving in production

I am using windows 10 as OS in development environment and Ubuntu 18.04 (AWS) in production. I deployed my application recently (15 days) but now when I see the django is no more serving media and static files in the development server while it is running and serving perfectly in the production server (with DEBUG=True in both the servers). I am using Nginx server with gunicorn at the production server.
I have tried almost every answer in the StackOverflow to counter this issue but it is not working.
settings.py:
# MEDIA:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
...
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
# STATICFILES_DIRS = ('static', )
#STATICFILES_DIRS = (os.path.join('static'), )
main_project/urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings # new
from django.conf.urls.static import static # new
urlpatterns = [
path('', include('stock_management.urls', namespace='stock_management')),
path('auth/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
]
# if settings.DEBUG: # new
# urlpatterns += static(settings.STATIC_URL,
# document_root=settings.STATIC_ROOT)
# urlpatterns += static(settings.MEDIA_URL,
# document_root=settings.MEDIA_ROOT)
app/urls.py:
from django.urls import path, include
from .views import *
from django.conf import settings
app_name = 'stock_management'
urlpatterns = [
# Stock:
path('', stock_list, name='homepage'),
path('stock/', stock_list, name='stock_list'),
path('stock/add', stock_create_view, name='add_stock'),
path('stock/<pk>/edit', stock_edit, name='stock_edit'),
# Item:
path('items/', item_list, name='item_list'),
path('item/<pk>/edit', item_edit, name='item_edit'),
path('item/<pk>/delete', item_delete, name='item_delete'),
# API
path('api/items', item_list_API, name='item_list_API'),
# Gallery:
path('items/gallery', item_gallery, name='item_gallery'),
]
# if settings.DEBUG:
# # test mode
# from django.conf.urls.static import static
# urlpatterns += static(settings.STATIC_URL,
# document_root=settings.STATIC_ROOT)
# urlpatterns += static(settings.MEDIA_URL,
# document_root=settings.MEDIA_ROOT)
I want a solution so that django can serve static and media files to my localhost also and at the same time when I commit any changes, it does not disturb the production enviornment.
EDIT:
I have uncommented the settings.DEBUG condition from both the urls.py files and now it is serving the media files but not static files in the local server.
if settings.DEBUG:
# test mode
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
if settings.DEBUG: # new
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
After much effort, I think I have found the answer.
In the development server the following configurations works:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('static', )
While in production server with Nginx properly set up to serve the static files the following configuration is enough:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
if you want to serve static files during development:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
this setting is the only thing you need in your setting file which i assume your STATIC_URL is defined as /static/ , you comment out those lines and it will work.
I took these lines from documentation. Hence you can use seperate settings files for production and development of django also. so one will have DEBUG=True while the other one is defined False, which I think that is the reason that your problem is occurs.
ps: according to your BASE_DIR setting. add two lines to your development settings in settings.py file
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
and for the urls.py I use these lines
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Static Files With Django Development Server 1.7 Slightly Different Directory Settings

This is my website directory:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\js
\css
\media
\templates
base.html
What I have added to my settings.py is:
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "templates"),
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static"),
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media"),
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static"),
),
and to my 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 hey, it does'nt work. The way I refer to them in the base.html is:
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
Any ideas?
Thank you.
Hasan
Running the settings that you've pasted gave me an error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting. Commenting-out os.path.join(os.path.dirname(BASE_DIR), "static"), in the settings removed the error and the template correctly found the CSS file without the URLs.py modifications.
First of all as #r---------k mentioned, I should not have had trailing commas except for the tuples. So the settings.py should look like:
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
And the second thing is that, as you may notice, STATIC_ROOT and STATICFILES_DIRS cannot possibly have the same value. I added another static folder inside the static folder and put my js and css folders inside of it:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
The directory looks like:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\static
\js
\css
\media
\templates
base.html
Finally, you should have the urlpatterns in your 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)
This seems to work in current django (2.2) in your urls.py:
from django.conf import settings
from django.conf.urls.static import serve
from django.urls import include, path
urlpatterns += [
path(settings.STATIC_URL[1:], serve, {'document_root': settings.STATIC_ROOT })
]
Remember to run ./manage.py collectstatic to collect the static files to settings.STATIC_ROOT

Django python - Module not found

I am a newbie so I might have done something stupid. running python 3.3 and Django 1.6.2.
When I run the local server via command line, this is the error I receive "P/1.1 404 1712" and error on the browser is "module not found" and the exception location direct me urls.py line 22;
document_root=settings.STATIC_ROOT)
this is a part of urls.py:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'signups.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
This is how my settings.py looks:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/whattheheck/static/'
# Template location
TEMPLATE_DIRS = {
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "templates"),
}
if DEBUG:
MEDIA_URL = '/whattheheck/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "media")
STATICFLIES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static")
)
Can someone help please?
You forgot one static in the import statement, see the documentation:
from django.conf.urls.static import static
# ^^^^^^ this one
Right now, it tries to use the static module as a function but obviously, it does not work. The error 'module' object is not callable is raised when you are trying to use a module object (for example os, sys or any third-party) as a callable (with a __call__ method).

Django - Serving Media/Admin files in production with Apache/Webfaction

As it stands my STATIC files are served without issue using the configuration below.
In my settings.py:
MEDIA_ROOT = '/home/chronic88/webapps/media_media/'
MEDIA_URL = '/media/'
STATIC_ROOT = '/home/chronic88/webapps/static_media/'
STATIC_URL = '/static/'
static_media and media_media are both applications served by apache.
I can upload files via the admin and they show up inside the folder media_media, but they won't display on their pages. When I check the file paths in the page source they seem correct mydomain.com/media/image.png but they simply won't display. So, it seems the link is there but there is some problem communicating between apache and django that I can't put my finger on.
And my main urls.py:
admin.autodiscover()
urlpatterns = patterns('',
url(r'^', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is the urls.py I'm using in prodution that works. I tried it without the last line in production but it gives the same result (files are uploaded but not displayable).
What am I missing?
If you configured your Apache to serve the static files from the respective root folders, you also have to run manage.py collectstatic (see docs).
To test static and media files I usually have this in my urls.py:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True}),
)

Categories