'Django update/render bloghome page using the form filter elements
Let us say I have an HTML page (blogHome.html) with different elements like this one,
<div class="container-fluid bg-light">
<div class="container">
<div class="row sm-3 justify-content-center">
<div class="col md-3 p-3 d-flex justify-content-center">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="vegetarian" name="vegetarian" checked>
<label for="vegetarian">Vegetarian</label>
</div>
</div>
<div class="col md-3 p-3 d-flex justify-content-center">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="nonvegetarian" name="nonvegetarian" checked>
<label for="nonvegetarian">Non-Vegetarian</label>
</div>
</div>
<div class="col md-3 p-3 d-flex justify-content-center">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="vegan" name="vegan" checked>
<label for="vegan">Vegan</label>
</div>
</div>
</div>
</div>
<div class="container">
<a href="#" class="btn btn-primary" type="submit">Get Filter Values</a>
</div>
<div class="container my-3">
<div class="row">
{% for post in allPosts %}
<div class="col-md-3">
<!-- HERE -->
<div class="card mb-4 shadow-sm">
<!-- HERE -->
<img class="card-img-top" src='{% static "img/tamilnadu.jpg" %}' alt="Card image cap">
<div class="card-body">
<strong class="d-inline-block mb-2 text-primary">Article by {{post.author}} ({{post.views}} views)</strong>
<h3 class="card-title"><a class="text-dark" href="/blog/{{post.slug}}"></a>{{post.title}}</h3>
<div class="mb-1 text-muted">{{post.timestamp}}</div>
<p class="card-text mb-auto"><div class="preview">{{post.content|safe|truncatechars:200}}</div></p>
</div>
</div><!-- /.card -->
</div><!-- /.col-md-3 -->
{% if forloop.counter|divisibleby:4 %}
</div>
<div class="row">{% endif %} {# HERE #}
{% endfor %}
</div><!-- /.row -->
This is how my urls.py looks like
urlpatterns = [
path('', views.blogHome, name="bloghome"),
]
My models.py file
class Post(models.Model):
sno = models.AutoField(primary_key=True)
title = models.CharField(max_length=255)
author = models.CharField(max_length=25)
content = models.TextField()
vegetarian = models.IntegerField(default=0)
nonvegetarian = models.IntegerField(default=0)
vegan = models.IntegerField(default=0)
Using Django admin page I have created many posts with integer field filled for vegetarian, nonvegetarins and vegan. As of now user can see all posts that I have stored in the database through django forms from my html page. Further i have 3 checkboxes (vegetarian, nonvegetarian and vegan). Now i would like to acheive following.
- After user clicks the button Update Blocks, it should get my filters (checkboxes here (vegetarian, nonvegetarian, vegan)) and update the django forms where i display only maybe vegetarin or nonvegetarin or vegan or all possible combinations.
My views.py file
def blogHome(request):
allPosts = Post.objects.all()
#vegetarian = request.GET.get('vegetarian')# Not working
#nonvegetarian = request.GET.get('nonvegetarian')# Not working
#vegan = request.GET.get('vegan')# Not working
context = {'allPosts' : allPosts}
return render(request, "blog/blogHome.html", context)
Please guide me to solve this issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

