简体   繁体   English

如何使用reactjs在django url中的url中获取媒体图像

[英]How to get media images in url in django url with reactjs

static and media configuration in settings.py settings.py 中的静态和媒体配置

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'frontend','build', 'static')
]

MEDIA_URL='/media/'

MEDIA_ROOT=os.path.join(BASE_DIR, 'media')

urls.py网址.py

from django.contrib import admin
from django.urls import path, include, re_path
from django.views.generic import TemplateView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('', TemplateView.as_view(template_name='index.html')),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

Here i am using django with reactjs.在这里,我将 Django 与 reactjs 一起使用。

i have given the path of build folder in react app like this.我已经在这样的反应应用程序中给出了构建文件夹的路径。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'frontend','build')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

When i am going to http://127.0.0.1:8000/media/images_OkG6q2k.jpeg path to get images in side media folder which are uploaded by users i am getting the react route page.当我要去http://127.0.0.1:8000/media/images_OkG6q2k.jpeg路径以获取用户上传的侧媒体文件夹中的图像时,我正在获取反应路线页面。 I am not getting the image from media folder.我没有从媒体文件夹中获取图像。

How i can see the media photos through url.我如何通过网址查看媒体照片。

Project structure项目结构

react home is coming instead media image like this when i amgoing url当我访问 url 时,react home 来了,而不是像这样的媒体图像

I think I just overcame this problem with one of my projects with Django backend + React frontend.我想我刚刚用 Django 后端 + React 前端的一个项目克服了这个问题。 It is in your url patterns.它在您的网址模式中。

As I understand it, this catch-all pattern:据我了解,这种包罗万象的模式:

re_path('', TemplateView.as_view(template_name='index.html'))

essentially overrides the media url pattern you wrote (and any others below it), which prevents the Django backend from allowing you to access the media files.本质上覆盖了您编写的媒体 url 模式(以及它下面的任何其他模式),这会阻止 Django 后端允许您访问媒体文件。 To fix this, simply make sure the catch-all route is the last one on your patterns.要解决此问题,只需确保所有路径是您模式中的最后一个。 After you are done with the rest of the patterns, on a new line do this:完成其余模式后,在新行上执行以下操作:

urlpatterns += [re_path('', TemplateView.as_view(template_name='index.html'))]

This way, you add it to your paths after all the rest and it doesn't "block" any of the others.这样,您将它添加到您的所有其他路径之后,它不会“阻止”任何其他路径。 hopefully this fixes it!希望这能解决它!

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

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