Django 上传并显示多张图片
Django Uplaod and show multiple images
这是我的模型 Post,我想为特定 post 上传多张图片,因此我创建了从 Post 图片到 Post 的外键。
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
# image = models.FileField(null = True, blank=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
#code for Thumbnail
# image = models.ImageField(upload_to = "media", default='DEFAULT VALUE')
image_thumbnail = ProcessedImageField(upload_to = "thumbnail",
processors = [ResizeToFill(100,50)],format = 'JPEG',options = {'quality':60},default='DEFAULT VALUE')
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
#code for uploading multiple images
class PostPicture(models.Model):
picture =models.ImageField(upload_to="blog_images", blank=True)
postid =models.ForeignKey(Post,on_delete=models.CASCADE,related_name='pictures')
这是我的模板代码,我在其中遍历图片并尝试显示它们。
{% for i in post.pictures.all %}
<img src = "{{ post.pictures.url }}" height = "200", width="200"/>
{% endfor %}
这是views.py
def post_detail(request, slug):
template_name = 'post_detail.html'
post = get_object_or_404(Post, slug=slug)
comments = post.comments.filter(active=True)
new_comment = None
# Comment posted
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request, template_name, {'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form}
)
我正在使用admin添加博客post,附上ss看看admin![
]1
这是 UI 上的输出,我没有让图像显示在这里。
我还添加了目录的屏幕截图:
任何人都可以帮助我,我哪里错了
模板不对
您在 for 循环中为 src
使用了不正确的变量:
{% for i in post.pictures.all %}
<img src = "{{ i.picture.url }}" height = "200", width="200"/>
{% endfor %}
这是我的模型 Post,我想为特定 post 上传多张图片,因此我创建了从 Post 图片到 Post 的外键。
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
# image = models.FileField(null = True, blank=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
#code for Thumbnail
# image = models.ImageField(upload_to = "media", default='DEFAULT VALUE')
image_thumbnail = ProcessedImageField(upload_to = "thumbnail",
processors = [ResizeToFill(100,50)],format = 'JPEG',options = {'quality':60},default='DEFAULT VALUE')
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
#code for uploading multiple images
class PostPicture(models.Model):
picture =models.ImageField(upload_to="blog_images", blank=True)
postid =models.ForeignKey(Post,on_delete=models.CASCADE,related_name='pictures')
这是我的模板代码,我在其中遍历图片并尝试显示它们。
{% for i in post.pictures.all %}
<img src = "{{ post.pictures.url }}" height = "200", width="200"/>
{% endfor %}
这是views.py
def post_detail(request, slug):
template_name = 'post_detail.html'
post = get_object_or_404(Post, slug=slug)
comments = post.comments.filter(active=True)
new_comment = None
# Comment posted
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request, template_name, {'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form}
)
我正在使用admin添加博客post,附上ss看看admin![ ]1
这是 UI 上的输出,我没有让图像显示在这里。
我还添加了目录的屏幕截图:
模板不对
您在 for 循环中为 src
使用了不正确的变量:
{% for i in post.pictures.all %}
<img src = "{{ i.picture.url }}" height = "200", width="200"/>
{% endfor %}