[英]How to access Django queryset when for loop is not possible
I'm making my portfolio site.我正在制作我的投资组合网站。 Each project is developed with multiple technologies, such as a "website" created with Python, Django and Bootstrap.每个项目都使用多种技术开发,例如使用 Python、Django 和 Bootstrap 创建的“网站”。
So I defined my models like this:所以我这样定义我的模型:
class Technology(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'Technologies'
class Project(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
date = models.DateField()
technology = models.ManyToManyField(Technology)
image = models.ImageField(upload_to='projects')
def __str__(self):
return self.name
views.py:视图.py:
def index(request):
projects = Project.objects.all()
return render(request, 'homepage/index.html', context={'projects': projects})
Now here's the challenge:现在是挑战:
In my template I want to display 6 projects with the project name and technologies extracted from the '''queryobject'''.在我的模板中,我想显示 6 个项目,其中项目名称和技术是从“查询对象”中提取的。 However right now I have only one project.但是现在我只有一个项目。 So:所以:
manytomany
field, there is more than one value in it (Python, Django, Bootstrap for example).由于“技术”字段是manytomany
字段,因此其中包含多个值(例如 Python、Django、Bootstrap)。 How to access it in my template?如何在我的模板中访问它?EDIT:编辑:
I feel I need to explain a little more.我觉得我需要多解释一点。 What I need for each project I have:我拥有的每个项目都需要什么:
<div class="col-lg-4 col-md-6 portfolio-item filter-python filter-django filter-bootstrap">
I'm making my portfolio site.我正在制作我的投资组合网站。 Each project is developed with multiple technologies, such as a "website" created with Python, Django and Bootstrap.每个项目都使用多种技术开发,例如使用 Python、Django 和 Bootstrap 创建的“网站”。
So I defined my models like this:所以我这样定义我的模型:
class Technology(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'Technologies'
class Project(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
date = models.DateField()
technology = models.ManyToManyField(Technology)
image = models.ImageField(upload_to='projects')
def __str__(self):
return self.name
views.py:视图.py:
def index(request):
projects = Project.objects.all()
return render(request, 'homepage/index.html', context={'projects': projects})
Now here's the challenge:现在是挑战:
In my template I want to display 6 projects with the project name and technologies extracted from the '''queryobject'''.在我的模板中,我想显示 6 个项目,其中项目名称和技术是从“查询对象”中提取的。 However right now I have only one project.但是现在我只有一个项目。 So:所以:
manytomany
field, there is more than one value in it (Python, Django, Bootstrap for example).由于“技术”字段是manytomany
字段,因此其中包含多个值(例如 Python、Django、Bootstrap)。 How to access it in my template?如何在我的模板中访问它?EDIT:编辑:
I feel I need to explain a little more.我觉得我需要多解释一点。 What I need for each project I have:我拥有的每个项目都需要什么:
<div class="col-lg-4 col-md-6 portfolio-item filter-python filter-django filter-bootstrap">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.