Django:使用for循环查看我的数据库中的所有图像-使用img src [英] Django: Using a for loop to view all images from my db - using img src
问题描述
我目前正在学习Django,并开始我的第一步.我尝试建立一个网络画廊来学习所有基本知识.我使用静态文件成功显示了一些图像.因此,我尝试通过ImageFields和"upload_to"将图像保存在数据库中,并将其保存到我的静态目录中.我试图用标签中的for循环显示每个人.使用{%static%}标签可以正确显示我的img,但是当我尝试插入{{}}标签时,尽管它是相同的URL,但它却无法正常工作.
I am currently learning Django and making my first steps. I try to build a webgallery to learn all the basic stuff. I successfully displayed some images using static files. So I tried saving Images through ImageFields and "upload_to" in my DB, saving it to my static directory. I tried to display everyone of them with a for loop in an tag. My img displays properly with using a {% static %} tag but when I try to insert a {{ }} Tag it isn't working, although it's the same url it doesn't work.
- 我尝试在settings.py中更改我的静态文件
- 我尝试了将{{}}嵌套在其中的各种其他形式
- 将文档读取到staticfile https://docs.djangoproject.com/en/2.2/howto/static-files/
- 此线程在Django中显示位于数据库中的图像
- 此线程 https://docs.djangoproject.com/en/2.2/topics/files/#using-files-in-models
我的代码:
<p>Overview</p>
{% block content %}
<div>
{% for image in images %}
{{ image.img_photo }} <!-- webgalleries/test.jpg -->
{% load static %}
<img src="{% static 'webgalleries/test.jpg' %}" alt="{{ image }}"> <!-- working -->
<img src="{% static '{{ image.img_photo }}' %}" alt="{{ image }}"> <!-- not working -->
{% empty %}
<p>No content</p>
{% endfor %}
</div>
{% endblock content %}
我希望输出是我的静态目录中的img.
I expect the output to be an img from my static directory.
高度赞赏提示,一些建议或其他形式的帮助.
A hint, some advice or other forms of help is highly appreciated.
非常感谢您!
推荐答案
好的,如果要显示数据库中的图像,则应执行以下步骤:
okay if you want to display images from database you should do these steps :
1-转到您的settings.py,并在此处编写此代码,
1- go to your settings.py and write this code there ,
MEDIA_ROOT= os.path.join(BASE_DIR,"media")
MEDIA_URL= "/media/"
2-然后在您的项目中创建一个名为"media"的新文件夹,并在"media"中创建一个名为"images"的文件夹(最终结果将类似于"media/images")
2- then create new folder in your project called 'media' and create folder inside 'media' called 'images' (finally result will be like this 'media/images' )
3-转到您班上有'img_photo'的model.py
3- go to your model.py in your class that having 'img_photo'
您应该像这样编写模型
class Images(models.Model):
img_photo = models.ImageField(upload_to='images/',null=True, blank=True)
def get_image(self):
if self.img_photo and hasattr(self.img_photo, 'url'):
return self.img_photo.url
else:
return '/path/to/default/image'
def __str__(self):
return self.img_photo
4-转到admin.py,然后输入:
4- go to admin.py then write :
from yourapp.models import Images
然后在下面添加此行
admin.site.register(图片)
admin.site.register(Images)
然后打开您的终端或控制台并输入:
then open your terminal or console and write :
1- python manage.py makemigrations
1- python manage.py makemigrations
2- python manage.py migration
2- python manage.py migrate
5-必须使用html代码编写:
5- in html code you must write :
{% for image in Images %}
<img src="{{ image.get_image }}" >
{% endfor %}
- 转到管理面板并上传任何照片进行测试
这篇关于Django:使用for循环查看我的数据库中的所有图像-使用img src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!