0

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)

11
  • Can you share your University model? Commented Jun 24, 2019 at 20:31
  • 1
    @Skalfaro: I would really advice you to follow the Django tutorial. It explains some basics about webdevelopment, HTTP, etc. Usually with these concepts a lot is more clear. Commented Jun 24, 2019 at 20:36
  • 2
    @WillemVanOnsem, yes, that's what I meant. He appears to know how to use 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 appropriate QuerySet back. 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). Commented Jun 24, 2019 at 20:46
  • 2
    @Skalfaro: the Django website has a tutorial, that is, imho, quite good: docs.djangoproject.com/en/2.2/intro/tutorial01 it is a "hands on" tutorial, you create a "mini-blog" website. Commented Jun 24, 2019 at 21:04
  • 3
    @Skalfaro, always use official tutorials when possible. Generally speaking, YouTube is a terrible medium / source for tutorials. Commented Jun 24, 2019 at 21:34

1 Answer 1

1

You can make a query with:

def index_u(request):
    region = request.GET.get('region')
    universities = University.objects.all()
    if region is not None:
        universities = universities.filter(region=region)
    return render(request, 'index_uni.html', {'universities': universities})

You can then create for example a form, like:

<form method="get" action="url-of-index_u">
    <input type="text" name="region">
    <input type="submit">
</form>
<! --  render the universities -->
Sign up to request clarification or add additional context in comments.

1 Comment

@funnydman: by using indentation, the boldface of the .filter(region=region) disappears.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.