I have a web page built in django that shows all rows of a sqlite table in bootstrap cards. Basically each card is a row of the table. I want to give the user the ability to filter these cards, so the page instead of showing every objects in the database's table should display only the objects relevant to the user. The result of the search can be displayed at another URL, it's irrelevant.
I can build a html form to get info from the user and I can write a python function that search the database and print out specific rows based on an input keyword, but I can't make these things work together.
Any hint or piece of code would be a great help, unfortunately I know nothing about php so if there's a way to do so without it would be great. Thanks all.
# this is my view function, it shows all the objects in the table at a #specific URL
def index_u(request):
universities = University.objects.all()
return render(request, 'index_uni.html', {'universities': universities})
/* this is index_uni.html */
{% block content %}
<div class="row">
{% for university in universities %}
<div class="col">
<div class="card" style="width: 18rem;">
<img src="{{ university.image_url}}" class="card-img-top">
<div class="card-body">
<h5 class="card-title">{{university.name}}</h5>
<p class="card-text">{{university.region}}</p>
<a href="{{university.page_url}}" target="_blank">Go</a>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
#this function takes an input and diplays specific rows
#I don't know how make this works in the html page, it works fine in terminal
def search_data():
region_choice = input("insert region here: ")
command = "SELECT name FROM university_university WHERE region = ?;"
c.execute(command, [region_choice])
for row in c:
print(row)
#here my model
class University(models.Model):
name = models.CharField(max_length=58, default=True)
city = models.CharField(max_length=58, default=True)
region = models.CharField(max_length=58, default=True)
country = models.CharField(max_length=58, default=True)
number_of_students = models.IntegerField(default=True)
image_url = models.CharField(max_length=2083, default=True)
is_public = models.BooleanField(default=True)
ranking = models.IntegerField(default=True)
colleges = models.BooleanField(default=True)
scholarship = models.BooleanField(default=True)
uni_canteen = models.BooleanField(default=True)
page_url = models.CharField(max_length=2083, default=True)
Universitymodel?University.objects.all(). He's got an SQL query but doesn't know how to use the result. A good solution is to do an ORM query and get an appropriateQuerySetback. I'm suggesting that that's what he should have asked (though as you pointed out a trip through the tutorial would do a world of good here).