简体   繁体   English

在 Django 模板中检索用户上传的图像

[英]Retrieving images uploaded by users in Django Templates

I understand there are multiple (if not 100s) of questions pertaining to my issue.我了解有多个(如果不是 100 多个)与我的问题有关的问题。 After trying all, I am here finally to ask.尝试了所有之后,我终于在这里问了。 I am able to upload the image and the image path in the model.我可以上传 model 中的图像和图像路径。 Example of an image field from model:来自 model 的图像字段示例:

<ImageFieldFile: static/image1_FzGpiKx.jpeg>

My static folder is right where the project and app folders are.我的 static 文件夹就在项目和应用程序文件夹所在的位置。 In similar hierarchy.在类似的层次结构中。 I have the following settings in my settings.py file:我的 settings.py 文件中有以下设置:

MEDIA_ROOT = '/static/'
MEDIA_URL = '/'

In my app level urls.py, here is what I have for rendering these images:在我的应用程序级别 urls.py 中,这是我用于渲染这些图像的内容:

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

After all this settings, my template shows the following as img src:完成所有这些设置后,我的模板将以下内容显示为 img src:

/static/image1_FzGpiKx.jpeg

Here is how I render in template:这是我在模板中呈现的方式:

<img src={{article.image.url}} />

Yet it just renders the typical broken image icon.然而它只是呈现典型的破碎图像图标。 Can someone help me here?有人可以在这里帮助我吗? Thank you!谢谢!

MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/static/'

use this in your settings file, and upload a new image and then check it在您的设置文件中使用它,并上传新图像然后检查它

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

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