简体   繁体   English

Django Rest应用未正确使用媒体根来提供图像

[英]Django Rest app not using media root correctly to serve images

I have a project that has 3 apps, one named stores with store models, products with product models, and api which is the rest framework app that serves the Json results to clients. 我有一个项目,其中包含3个应用程序,一个名为商店模型的商店,具有产品模型的产品以及api(这是为客户提供Json结果的其余框架应用程序)。 I set the media root in settings.py as MEDIA_ROOT = '/photos/' and the upload works for both product and store models. 我在settings.py中将媒体根目录设置为MEDIA_ROOT = '/photos/' ,并且上传适用于产品和商店模型。 The main problem here is that for some reason the rest framework returns a url that references the api app instead of the products or stores apps for the media root url. 这里的主要问题是,由于某种原因,其余框架返回的URL引用的是api应用程序而不是产品或存储应用程序的媒体根URL。 here are my models 这是我的模特

class Product(models.Model):

def get_image_path(instance, filename):
    return '/Products/' + filename

picture = models.ImageField(width_field=None, max_length=100, blank =True, null =True)

store: 商店:

class Store(models.Model):


def __str__(self):
    return self.name


def get_image_path(instance, filename):
    return os.path.join('productphotos', 'stores', filename)

picture = models.ImageField(width_field=None, max_length=100, blank =True, null =True)

How do i set the mediaroot to the project directory instead so that all apps in the project reference it as mediaroot instead of themselves? 如何将mediaroot设置为项目目录,以便项目中的所有应用都将其引用为mediaroot而不是它们自己?

The upload works and upoads the pictures to the instructed directories in the root of the project (where manage.py is found), but the rest framework thinks it should get the media from the api app.. what's the proper way of doing this? 上载有效,并将图片上载到项目根目录中的指示目录(在其中找到manage.py),但是其余框架认为它应该从api应用程序中获取媒体。.这样做的正确方法是什么? here are screenshots: 这是屏幕截图:

the path uploaded to 上传到的路径 成功上传路径

the path returned in json json中返回的路径 DRF返回的路径

The MEDIA_URL setting is the URL path in the browser. MEDIA_URL设置是浏览器中的URL路径。 The MEDIA_ROOT setting is the root directory on your server, and should be an absolute path. MEDIA_ROOT设置是服务器上的根目录,并且应该是绝对路径。

MEDIA_URL = '/pictures/'

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded_pictures')

Also, if you want Product and Store pictures to go into different sub directories, for example pictures/products/ and pictures/store/ , you'd need to set the upload_to argument on the model field. 另外,如果您希望“ Product和“ Store图片进入不同的子目录,例如pictures/products/pictures/store/ ,则需要在模型字段上设置upload_to参数。 For example 例如

picture = models.ImageField(upload_to='products/', ... )

Edit: To serve static and media files during development, add this at the end of the urls.py 编辑:要在开发过程中提供静态文件和媒体文件,请将其添加到urls.py的末尾

if settings.DEBUG:
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    from django.conf.urls.static import static

    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(
        settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

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

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