简体   繁体   English

如何同时使用静态文件和媒体

[英]How to use both static files and media together

Is it possible to use both static files and media in a project?是否可以在项目中同时使用静态文件和媒体? because all tutorials use only one of them.因为所有教程只使用其中一个。

MEDIA_URL= 'media/'
MEDIA_ROOT = BASE_DIR / 'media'

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'static/'
STATICFILES_DIRS = BASE_DIR / 'static/

I wrote this to setting.我把这个写到设置中。 How am supposed to modify urls.py ?应该如何修改urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('pages.urls')),
    path('users/',include('users.urls')),
    path('users/',include('django.contrib.auth.urls')),
    ] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

I wrote it this way but how should I add static urls?我是这样写的,但是我应该如何添加静态网址?

You add the two lists generated by the static functions, so:您添加static函数生成的两个列表,因此:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('pages.urls')),
    path('users/', include('users.urls')),
    path('users/', include('django.contrib.auth.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Note however that Django does not serve static or media files in production, and that you this will have to set up nginx, apache, or another webserver to do so.但请注意,Django 在生产环境中提供静态或媒体文件,您必须设置 nginx、apache 或其他网络服务器才能这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM