使用Django Webpack Loader加载MEDIA_ROOT文件 [英] Loading MEDIA_ROOT files with Django Webpack Loader

查看:87
本文介绍了使用Django Webpack Loader加载MEDIA_ROOT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 django webpack加载器设置 vue 前端之后,媒体文件没有呈现.

After I setup a vue frontend with a django webpack loader, the media files are not rendering.

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 进行前端视图.应该如何解决此问题?

But when I browse the media file url, image isn't rendering. The issue lies inside urlpatterns at this line re_path(r'^.*$', IndexTemplateView.as_view(), name="entry-point") when I comment out this part. I can see the image. But i need the re_path for the frontend view. How should fix this issue?

推荐答案

您需要提供媒体文件,然后将其添加到 urls.py

you need to serve your media files add this to your urls.py

from django.views.static import serve

urlpatterns += [
    re_path(r'^media/(?P<path>.*)$', serve, {
        'document_root': settings.MEDIA_ROOT
    }),
]

这篇关于使用Django Webpack Loader加载MEDIA_ROOT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆