使用 Django Webpack Loader 加载 MEDIA_ROOT 个文件
Loading MEDIA_ROOT files with Django Webpack Loader
在我使用 django webpack loader
设置 vue
前端后,媒体文件未呈现。
settings.py
STATIC_URL = '/static/'
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
WEBPACK_LOADER = {
"DEFAULT": {
"BUNDLE_DIR_NAME": "dist/",
"STATS_FILE": os.path.join(BASE_DIR, "frontend", "webpack-stats.json"),
}
}
和urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include("companies.routes.urls")),
path('api/v2/', include("projects.routes.urls")),
path('accounts/register/', RegistrationView.as_view(
form_class=CompanyUserForm,
success_url="/",
), name="django_registration_register"),
path('accounts/', include("django_registration.backends.one_step.urls")),
path('accounts/', include("django.contrib.auth.urls")),
path('api-auth/', include("rest_framework.urls")),
path('api/rest_auth/', include("rest_auth.urls")),
path('api/rest_auth/django_registration/', include("rest_auth.registration.urls")),
re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point")
]
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
# adding the media root path.
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
但是当我浏览媒体文件 url 时,图像没有呈现。当我注释掉这部分时,问题出在 urlpatterns 这一行 re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point")
内。我可以看到图像。但我需要 re_path
作为前端视图。应该如何解决这个问题?
您需要提供您的媒体文件,将其添加到您的 urls.py
from django.views.static import serve
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
在我使用 django webpack loader
设置 vue
前端后,媒体文件未呈现。
settings.py
STATIC_URL = '/static/'
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
WEBPACK_LOADER = {
"DEFAULT": {
"BUNDLE_DIR_NAME": "dist/",
"STATS_FILE": os.path.join(BASE_DIR, "frontend", "webpack-stats.json"),
}
}
和urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include("companies.routes.urls")),
path('api/v2/', include("projects.routes.urls")),
path('accounts/register/', RegistrationView.as_view(
form_class=CompanyUserForm,
success_url="/",
), name="django_registration_register"),
path('accounts/', include("django_registration.backends.one_step.urls")),
path('accounts/', include("django.contrib.auth.urls")),
path('api-auth/', include("rest_framework.urls")),
path('api/rest_auth/', include("rest_auth.urls")),
path('api/rest_auth/django_registration/', include("rest_auth.registration.urls")),
re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point")
]
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
# adding the media root path.
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
但是当我浏览媒体文件 url 时,图像没有呈现。当我注释掉这部分时,问题出在 urlpatterns 这一行 re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point")
内。我可以看到图像。但我需要 re_path
作为前端视图。应该如何解决这个问题?
您需要提供您的媒体文件,将其添加到您的 urls.py
from django.views.static import serve
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]