简体   繁体   中英

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.

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:

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'''. 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? So the first project shows the data from database, and the rest 5 show a "coming soon" (please refer to attached screenshot)? And later when I add the second and third projects to the database, the output update itself accordingly?
  2. Since the "technology" field is a manytomany field, there is more than one value in it (Python, Django, Bootstrap for example). 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. 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.

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:

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'''. 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? So the first project shows the data from database, and the rest 5 show a "coming soon" (please refer to attached screenshot)? And later when I add the second and third projects to the database, the output update itself accordingly?
  2. Since the "technology" field is a manytomany field, there is more than one value in it (Python, Django, Bootstrap for example). 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. It looks like this:
<div class="col-lg-4 col-md-6 portfolio-item filter-python filter-django filter-bootstrap">

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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