1

I am learning django and I am currently working on a blog project. I have a basic search function which filters the posts accordingly. It works fine by searching for content in my title and content filters of the posts. I am trying to add a new filter so that I can type in the month for example "March" and then it will filter out all the posts that were published in March. All the models of Post(just a normal post in a blog) has this:

models.py

class Post(models.Model):
    title = models.CharField(max_length=120)
    content = models.TextField()
    publish = models.DateField(auto_now=False, auto_now_add=False)

And in my views.py I have the following search function: (only the relevant stuff)

views.py

queryset_list = Post.objects.all()
query = request.GET.get("q")
    if query:
        queryset_list = queryset_list.filter(
        Q(title__icontains=query) |
        Q(content__icontains=query) |
        Q(publish__icontains=query)
        ).distinct()

In the template the publish attribute is displayed like this: "July 12, 2016" if the publish date is set to 2016-07-12.

Like I mentioned the search function filters the posts fine when the title or content is = to the query, but it does not work if I search for "July". Then it doesn't return any posts.

I would like to know what I can do so it can filter the posts by the publish date as well.

1 Answer 1

0

I found a solution. I realized that if I search for "07" instead of July it filters out the posts accordingly, so currently that is solving my problem since the blog will only be used by my at the moment. It can obviously be converted to the month word with some python.

This is because you enter the DateField in the format yyyy/mm/dd. Thus Post.publish will also be in this format.

Not the answer you're looking for? Browse other questions tagged or ask your own question.