簡體   English   中英

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服務器。

我已經嘗試了StackOverflow中的幾乎所有答案來解決此問題,但是它不起作用。

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)

應用程序/ 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的productiondevelopment 因此一個將具有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/'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM