(I don't know what happened to original post.)
Suppose I have two images, "\media\images\image1.jpg" and
"\media\images\image2.jpg". I want to display them in a blog post that I've
written. I thought I should create a model for images but with multiple
images I cannot figure out how to add them to the body of my blog post. It
seems like I have to write full locations to the body like this:
<img src="\media\images\image1.jpg" alt="My Desrciption" />
instead of something like this:
<img src="{{ post.images.image.url[1] }}" alt="{{ post.images.image.url[2]
}}" />
I prefer a method like the latter one.
Here is my app structure:
# models.py:
class Post(models.Model):
title = models.CharField(max_length=250)
body = models.TextField()
slug = models.SlugField(max_length=250)
publish = models.DateTimeField(default=timezone.now)
class Images(models.Model):
post = models.ForeignKey(Post, default=None, related_name='images')
description = models.TextField()
image = models.ImageField()
# views.py:
def post_detail_view(request, year, month, day, postslug):
post = get_object_or_404(Post,
slug=postslug,
publish__year=year,
publish__month=month,
publish__day=day
)
return render(request=request,
template_name='blogapp/post/detail.html',
context={'post': post})
# detail.html:
{% extends "blogapp/base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
{{ post.body|safe }}
{% endblock %}
post.body = """
<h2>Example blog post</h2>
<p>Here is some text. And image for this part:</p>
<img src="{{ post.image1.filename }}" alt="{{ post.image1.description }}"
/>
<p>Here is some other text and image for this part:</p>
<img src="{{ post.image2.filename }}" alt="{{ post.image2.description }}"
/>
<p>I'm ending my blog post here.</p>
"""
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/2f098a0a-d711-44af-b22a-058447872f22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.