Template syntax error. Could not parse the remainder (Django)

3.5K    Asked by AugustinaFuentes in Python , Asked on Jul 19, 2021

 Can someone point me to the right direction? Below is what I have been doing

    

{{article.snippet}}


    

{{article.date}}


    

{{article.retrieve_by_category("web")}}

I have no problems getting the date or the snippet but I keep getting the following error when trying to run article.retireve_by_category.

Could not parse the remainder: '("web")' from 'article.retrieve_by_category("web")'

Below is the code for the model:

class Article(models.Model):
    category = models.CharField(max_length=100)
    title = models.CharField(max_length=100)
    slug = models.SlugField()
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    #add in thumbnail
    def __str__(self):
        return self.title
    def snippet(self):
        return self.body[:500] + "..."
    def retrieve_by_category(self,category):
        """retrieves blogs based on their category"""
        if (str(self.category) == category):
            return self.category
        return ''

Answered by Carl Paige

To resolve could not parse the remainder, you can't call functions inside the template. It's best to handle it in your views ,Below is a simple example:

def your_view(request):
   article = Article.objects.filter(category="web")
   return render(request, "/toyourtemplate", {'web':article})

If you want to make the category dynamic user input, you can use django forms



Your Answer

Interviews

Parent Categories