How to access Django queryset when for loop is not possible

  django, django-models, django-templates, python

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?

enter image description here

Source: Python Questions

LEAVE A COMMENT