具有多对多关系的模型的 Django 访问字段
Django access fields of model with many to many relationship
所以我有以下型号:
class Image(models.Model):
image=models.ImageField(upload_to='postimages')
id=models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
class Post(models.Model):
title=models.CharField(max_length=500)
created_date=models.DateField(auto_now=True)
id=models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
images=models.ManyToManyField(Image)
user=models.ForeignKey(get_user_model(), on_delete=models.CASCADE, null=True, related_name='posts')
在我看来,我创建了一个 post 对象,如下所示:
post=Post(title=title)
post.save()
post.images.add(image)
现在我需要在我的主页中显示图像模型的图像字段。我正在尝试这样做:
{%for post in posts%}
<img src="{{post.images.image}}">
{%endfor%}
但是这张 returns 图片的 src=(未知)。所以我的问题是如何访问图像模型的图像字段?
编辑:
这是我的 views.py
def addpost(request):
imageform=ImageForm()
postform=PostForm()
if request.method=="POST":
imageform=ImageForm(request.POST, request.FILES)
postform=PostForm(request.POST)
if imageform.is_valid() and postform.is_valid():
#add the image and the post to the database
image=Image(image=request.FILES['image'])
image.save()
title=request.POST['title']
post=Post(title=title)
post.save()
post.images.add(image)
return redirect('../')
还有我的表格:
<form method="post" action="{%url 'addpost'%}" enctype="multipart/form-data">
{%csrf_token%}
{{imageform}}
{{postform}}
<button type="submit">Post</button>
</form>
我找到了修复方法。在我的 html 中,我正在调用 {{post.images.image}}。相反,我需要调用 {{post.images.all}} 来获取所有图像模型,然后我需要为每个模型获取图像。所以而不是
{%for post in posts%}
<img src="{{post.images.image}}">
<p>{{post.title}}</p>
{%endfor%}
我需要做
{%for post in posts%}
{%for image in post.images.all%}
<img src="{{image.image}}">
{%endfor%}
<p>{{post.title}}</p>
{%endfor%}
所以我有以下型号:
class Image(models.Model):
image=models.ImageField(upload_to='postimages')
id=models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
class Post(models.Model):
title=models.CharField(max_length=500)
created_date=models.DateField(auto_now=True)
id=models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
images=models.ManyToManyField(Image)
user=models.ForeignKey(get_user_model(), on_delete=models.CASCADE, null=True, related_name='posts')
在我看来,我创建了一个 post 对象,如下所示:
post=Post(title=title)
post.save()
post.images.add(image)
现在我需要在我的主页中显示图像模型的图像字段。我正在尝试这样做:
{%for post in posts%}
<img src="{{post.images.image}}">
{%endfor%}
但是这张 returns 图片的 src=(未知)。所以我的问题是如何访问图像模型的图像字段?
编辑: 这是我的 views.py
def addpost(request):
imageform=ImageForm()
postform=PostForm()
if request.method=="POST":
imageform=ImageForm(request.POST, request.FILES)
postform=PostForm(request.POST)
if imageform.is_valid() and postform.is_valid():
#add the image and the post to the database
image=Image(image=request.FILES['image'])
image.save()
title=request.POST['title']
post=Post(title=title)
post.save()
post.images.add(image)
return redirect('../')
还有我的表格:
<form method="post" action="{%url 'addpost'%}" enctype="multipart/form-data">
{%csrf_token%}
{{imageform}}
{{postform}}
<button type="submit">Post</button>
</form>
我找到了修复方法。在我的 html 中,我正在调用 {{post.images.image}}。相反,我需要调用 {{post.images.all}} 来获取所有图像模型,然后我需要为每个模型获取图像。所以而不是
{%for post in posts%}
<img src="{{post.images.image}}">
<p>{{post.title}}</p>
{%endfor%}
我需要做
{%for post in posts%}
{%for image in post.images.all%}
<img src="{{image.image}}">
{%endfor%}
<p>{{post.title}}</p>
{%endfor%}