Django Rest 应用程序未正确使用媒体根来提供图像
Django Rest app not using media root correctly to serve images
我有一个包含 3 个应用程序的项目,一个名为 store with store models,products with product models,api 是为客户提供 Json 结果的其余框架应用程序。我将 settings.py 中的媒体根设置为 MEDIA_ROOT = '/photos/'
并且上传适用于产品和商店模型。这里的主要问题是,出于某种原因,其余框架 returns 引用 api 应用程序而不是媒体根 url 的产品或存储应用程序的 url。这是我的模型
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)
商店:
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)
如何将 mediaroot 设置为项目目录,以便项目中的所有应用程序都将其引用为 mediaroot 而不是它们自己?
上传有效并将图片上传到项目根目录中的指示目录(在其中找到 manage.py),但其余框架认为它应该从 api 获取媒体应用程序..这样做的正确方法是什么?这是屏幕截图:
上传到的路径
json中返回的路径
MEDIA_URL
设置是浏览器中的URL路径。 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')
此外,如果您希望Product
和Store
图片进入不同的子目录,例如pictures/products/
和pictures/store/
,您需要设置upload_to
模型字段的参数。例如
picture = models.ImageField(upload_to='products/', ... )
编辑: 要在开发期间提供静态和媒体文件,请在 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)
我有一个包含 3 个应用程序的项目,一个名为 store with store models,products with product models,api 是为客户提供 Json 结果的其余框架应用程序。我将 settings.py 中的媒体根设置为 MEDIA_ROOT = '/photos/'
并且上传适用于产品和商店模型。这里的主要问题是,出于某种原因,其余框架 returns 引用 api 应用程序而不是媒体根 url 的产品或存储应用程序的 url。这是我的模型
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)
商店:
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)
如何将 mediaroot 设置为项目目录,以便项目中的所有应用程序都将其引用为 mediaroot 而不是它们自己?
上传有效并将图片上传到项目根目录中的指示目录(在其中找到 manage.py),但其余框架认为它应该从 api 获取媒体应用程序..这样做的正确方法是什么?这是屏幕截图:
上传到的路径
json中返回的路径
MEDIA_URL
设置是浏览器中的URL路径。 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')
此外,如果您希望Product
和Store
图片进入不同的子目录,例如pictures/products/
和pictures/store/
,您需要设置upload_to
模型字段的参数。例如
picture = models.ImageField(upload_to='products/', ... )
编辑: 要在开发期间提供静态和媒体文件,请在 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)