Django 不在开发中提供静态和媒体文件,但在生产中提供服务
Django is not serving static and media files in development but it is serving in production
我在开发环境中使用 windows 10 作为 OS,在生产环境中使用 Ubuntu 18.04 (AWS)。我最近(15 天)部署了我的应用程序,但现在当我看到 django 不再在开发服务器中提供媒体和静态文件时,它是 运行 并且在生产服务器中完美地服务(DEBUG=True 在两者中服务器)。我在生产服务器上使用带有 gunicorn 的 Nginx 服务器。
我已经尝试了 Whosebug 中的几乎所有答案来解决这个问题,但它没有用。
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)
我想要一个解决方案,这样 django 也可以为我的本地主机提供静态和媒体文件,同时当我提交任何更改时,它不会干扰生产环境。
编辑:
我已经取消注释 urls.py 文件中的 settings.DEBUG 条件,现在它在本地服务器中提供媒体文件而不是静态文件。
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)
如果您想在开发期间提供静态文件:
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)
这个设置是你设置文件中唯一需要的东西,我假设你的 STATIC_URL
被定义为 /static/
,你注释掉这些行,它就会起作用。
我从文档中提取了这些行。因此,您也可以为 django 的 production
和 development
使用单独的设置文件。所以一个将有 DEBUG=True
而另一个定义为 False
,我认为这就是你的问题发生的原因。
ps:根据您的BASE_DIR
设置。在 settings.py
文件
中向您的开发设置添加两行
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
对于 urls.py 我使用这些行
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
经过一番努力,我想我找到了答案。
在开发服务器中,以下配置有效:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('static', )
在生产服务器中 Nginx 正确设置为静态文件提供服务 以下配置就足够了:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
我在开发环境中使用 windows 10 作为 OS,在生产环境中使用 Ubuntu 18.04 (AWS)。我最近(15 天)部署了我的应用程序,但现在当我看到 django 不再在开发服务器中提供媒体和静态文件时,它是 运行 并且在生产服务器中完美地服务(DEBUG=True 在两者中服务器)。我在生产服务器上使用带有 gunicorn 的 Nginx 服务器。
我已经尝试了 Whosebug 中的几乎所有答案来解决这个问题,但它没有用。
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)
我想要一个解决方案,这样 django 也可以为我的本地主机提供静态和媒体文件,同时当我提交任何更改时,它不会干扰生产环境。
编辑: 我已经取消注释 urls.py 文件中的 settings.DEBUG 条件,现在它在本地服务器中提供媒体文件而不是静态文件。
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)
如果您想在开发期间提供静态文件:
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)
这个设置是你设置文件中唯一需要的东西,我假设你的 STATIC_URL
被定义为 /static/
,你注释掉这些行,它就会起作用。
我从文档中提取了这些行。因此,您也可以为 django 的 production
和 development
使用单独的设置文件。所以一个将有 DEBUG=True
而另一个定义为 False
,我认为这就是你的问题发生的原因。
ps:根据您的BASE_DIR
设置。在 settings.py
文件
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
对于 urls.py 我使用这些行
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
经过一番努力,我想我找到了答案。 在开发服务器中,以下配置有效:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('static', )
在生产服务器中 Nginx 正确设置为静态文件提供服务 以下配置就足够了:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'