简体   繁体   English

无法进行 for 循环时如何访问 Django 查询集

[英]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:所以:

  1. How to display one set of HTML tags for first project and another set (placeholders) for the rest?如何为第一个项目显示一组 HTML 标签和为 rest 显示另一组(占位符)? So the first project shows the data from database, and the rest 5 show a "coming soon" (please refer to attached screenshot)?那么第一个项目显示来自数据库的数据,rest 5 显示“即将推出”(请参阅随附的屏幕截图)? And later when I add the second and third projects to the database, the output update itself accordingly?后来当我将第二个和第三个项目添加到数据库时,output 会相应地更新自己吗?
  2. Since the "technology" field is a 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:我拥有的每个项目都需要什么:

  1. Project Name项目名称
  2. First technology第一技术
  3. All technologies - This is needed for a javascript library I use to filter out projects based on technology.所有技术 - 这是一个 javascript 库所需要的,我用来根据技术过滤掉项目。 It looks like this:它看起来像这样:
<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:所以:

  1. How to display one set of HTML tags for first project and another set (placeholders) for the rest?如何为第一个项目显示一组 HTML 标签和为 rest 显示另一组(占位符)? So the first project shows the data from database, and the rest 5 show a "coming soon" (please refer to attached screenshot)?那么第一个项目显示来自数据库的数据,rest 5 显示“即将推出”(请参阅随附的屏幕截图)? And later when I add the second and third projects to the database, the output update itself accordingly?后来当我将第二个和第三个项目添加到数据库时,output 会相应地更新自己吗?
  2. Since the "technology" field is a 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:我拥有的每个项目都需要什么:

  1. Project Name项目名称
  2. First technology第一技术
  3. All technologies - This is needed for a javascript library I use to filter out projects based on technology.所有技术 - 这是一个 javascript 库所需要的,我用来根据技术过滤掉项目。 It looks like this:它看起来像这样:
<div class="col-lg-4 col-md-6 portfolio-item filter-python filter-django filter-bootstrap">

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM