简体   繁体   English

Azure 部署的 django 应用程序在生产中未在 Vuejs 前端显示媒体文件

[英]Azure deployed django app not displaying media files on Vuejs frontend in prooduction

These are the packages that I am using:这些是我正在使用的软件包:

Django==3.2
django-storages==1.12.3

I am trying to deploy a django REST API with Vuejs frontend on azure.我正在尝试在 azure 上部署带有 Vuejs 前端的 django REST API。 This is my directory structure for the django API.这是我的 django API 目录结构。

在此处输入图像描述

I have used djang-storages[azure] to use an azure container blob to store media files.我使用 djang-storages[azure] 来使用 azure 容器 blob 来存储媒体文件。 I went through a tutorial to setup the blob connection with django.我通过一个教程来设置与 django 的 blob 连接。 Some configuration that I did with settings.py are these我用 settings.py 做的一些配置是这些

Settings.py设置.py

MEDIA_LOCATION = "media"

AZURE_ACCOUNT_NAME = "my account name"
AZURE_ACCOUNT_KEY="my token"
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
AZURE_LOCATION="media"
AZURE_CONTAINER="media"

STATIC_LOCATION = "static"
STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
STATICFILES_STORAGE = 'storages.backends.azure_storage.AzureStorage'
DEFAULT_FILE_STORAGE = 'el.custom_azure.AzureMediaStorage'
AZURE_CONNECTION_TIMEOUT_SECS=100

and my custom_azure.py looks like this:我的 custom_azure.py 看起来像这样:

custom_azure.py custom_azure.py

from storages.backends.azure_storage import AzureStorage

class AzureMediaStorage(AzureStorage):
    account_name="eltechstorage"
    account_key="my token"
    azure_container="media"
    expiration_specs=None

urls.py网址.py

from django.contrib import admin
from django.urls import path,include
from django.contrib.staticfiles.urls import  staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
    path("",include("main.urls"))
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

When I am using azure blob container to upload media files it is working perfectly in development environment and when I am testing the API from the deployed url, it is giving me the path of the file as expected on which if i go to, downloads the file for me, everything is working perfect.当我使用 azure blob 容器上传媒体文件时,它在开发环境中运行良好,当我从部署的 url 测试 API 时,它会按预期为我提供文件的路径,如果我去,下载为我归档,一切正常。 Below is attached a sample response from the API.下面附上了来自 API 的示例响应。 在此处输入图像描述

The Problem问题

But when I use the deployed version of both Django and Vuejs App, and I open the page, where the API returns the object containing link to the file.但是,当我使用 Django 和 Vuejs App 的部署版本并打开页面时,API 返回包含文件链接的对象。 it displays a 404 error.它显示 404 错误。

according to django doc, the static helper is only workable in debug mode.根据django doc,静态助手只能在调试模式下工作。 therefore, in your production environment, if you don't have debug mode enabled, then you static view will not be there.因此,在您的生产环境中,如果您没有启用调试模式,那么您的静态视图将不存在。 as a temporary measure, you can enable debug by put DEBUG=True in your application settings.作为临时措施,您可以通过在应用程序设置中设置DEBUG=True来启用调试。 as recommendation by django , you should serve your static files in a dedicate server in production environment.作为django的建议,您应该在生产环境中的专用服务器中提供静态文件。

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

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