Skip to content
Advertisement

Invalind linking throws ‘id’ expected a number but got ‘{{ navbarList.0.category }}’

I have probably a very simple error but I can’t figure it out.

I have a category model that looks like this. Navbar field is for putting the category name and link to it in the navbar section on the page.

class Category(models.Model):
    category = models.CharField(max_length=20,unique=True)
    navbar = models.BooleanField(null=False, blank=False, default=True)

    def __str__(self):
        return self.category

Due to menu layout looking like this I need to set nav links manually

nav - nav - language_changer - nav - nav

So I’ve set them like this

<a href='{% url "category" category="{navbarList.0.id }" %}'>
  {{ navbarList.0.category }}
</a>
<a href='{% url "category" category="{navbarList.1.id }" %}'>
  {{ navbarList.1.category }}
</a>

And this throws error

ValueError at /category/{{ navbarList.0.id }}/
Field 'id' expected a number but got '{{ navbarList.0.id }}'.

Advertisement

Answer

You should not wrap variables included in tags with braces, remove them and reference the variable without any wrapper

<a href='{% url "category" category=navbarList.0.id %}'>
Advertisement