[英]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. 我在开发环境中使用Windows 10作为OS,在生产环境中使用Ubuntu 18.04(AWS)。 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). 我最近(15天)部署了我的应用程序,但是现在我看到django不再在开发服务器中运行并且可以在生产服务器中完美运行时在开发服务器中提供媒体和静态文件(两个服务器中均为DEBUG = True)。 I am using Nginx server with gunicorn at the production server. 我在生产服务器上使用带有gunicorn的Nginx服务器。
I have tried almost every answer in the StackOverflow to counter this issue but it is not working. 我已经尝试了StackOverflow中的几乎所有答案来解决此问题,但是它不起作用。
settings.py: 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: 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: 应用程序/ 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. 我想要一个解决方案,以便django也可以将静态文件和媒体文件提供给本地主机,并且在我进行任何更改的同时,它不会干扰生产环境。
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. 编辑:我已经从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)
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. 此设置是您在设置文件中唯一需要的,我假设您的STATIC_URL
定义为/static/
,然后注释掉这些行,它将起作用。
I took these lines from documentation. 我从文档中摘录了这些内容。 Hence you can use seperate settings files for production
and development
of django also. 因此,您也可以将单独的设置文件用于django的production
和development
。 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. 因此一个将具有DEBUG=True
而另一个将被定义为False
,我认为这就是发生您的问题的原因。
ps: according to your BASE_DIR
setting. ps:根据您的BASE_DIR
设置。 add two lines to your development settings in settings.py
file 在settings.py
文件中的开发设置中添加两行
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 对于urls.py,我使用这些行
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
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: 在Nginx已正确设置为生产静态文件的生产服务器中,以下配置就足够了:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.