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

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 13:10:30

问题


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)


回答1:


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)



回答2:


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/'


来源:https://stackoverflow.com/questions/57346918/django-is-not-serving-static-and-media-files-in-development-but-it-is-serving-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!