Explore Courses Blog Tutorials Interview Questions
0 votes
in Web Technology by (19.9k points)

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




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 ''

1 Answer

0 votes
by (25.1k points)

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

Browse Categories