# How to use Django to search database and display info in web application?

**URL:** <https://forum.djangoproject.com/t/how-to-use-django-to-search-database-and-display-info-in-web-application/19464>\
**Category:** Getting Started\
**Created:** [March 13, 2023, 9:43am UTC](https://forum.djangoproject.com/t/how-to-use-django-to-search-database-and-display-info-in-web-application/19464 "2023-03-13T09:43:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Champ-Wei](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/champ-wei/32/12662_2.png) [@Champ-Wei](https://forum.djangoproject.com/u/Champ-Wei)\
**Post date:** [March 13, 2023, 9:43am UTC](https://forum.djangoproject.com/t/how-to-use-django-to-search-database-and-display-info-in-web-application/19464/1 "2023-03-13T09:43:29Z")

</div>

I’m currently working on a data visualization board.

 ![2023-03-13 (4)](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/3/3bf81fdcd4131dc9359dc62aa14cbf0544cdc315.png)  
I know a few things about creating classes in models.py, objects in views.py, and putting the value of objects in an HTML file.

But the best fuzzy search I know is like this:

`isc_2022 = Isc.objects.filter(issue_date__startswith='2022').count()`

This only gets a count number, but now I want to put information in HTML file through Django. The information is specially selected and ordered like this:

 ![MySQL](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/0/0d44ea0b39242c905b816efaf9d6f9b1f9df0b5a.png)

It is not hard for Python to get a list from a database, here is how I do it:

```auto
import pymysql

connection = pymysql.connect(
    host="localhost",
    user='root',
    password='xxxxxx',
    database='project', )
cursor = connection.cursor()

cursor.execute("""SELECT company, COUNT(*) AS count 
FROM cc_statistic
GROUP BY company
ORDER BY count DESC
LIMIT 5;""")

myresult = cursor.fetchall()

mylist = []
for x in myresult:
  mylist.append(x)

print(mylist)

```

But how can Django do this and dynamically connect the values with my web application? And more importantly, is there any better way than asking on Stack Overflow to obtain knowledge like this? I find the official document for Django, like other documentation for other libraries, not easy to use. And they don’t always have the thing I want.

Thank you for spending time helping me. Wish us all the best, mate. : )

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 13, 2023, 10:22am UTC](https://forum.djangoproject.com/t/how-to-use-django-to-search-database-and-display-info-in-web-application/19464/2 "2023-03-13T10:22:24Z")

</div>

Have you actually worked your way thought either the [Official Django Tutorial](https://docs.djangoproject.com/en/4.0/intro/tutorial01/) or the [Django Girls Tutorial](https://tutorial.djangogirls.org/en/)? Those teach you how to build views containing data retrieved from the database.

> [@Champ-Wei](#):
>
> But how can Django do this and dynamically connect the values with my web application?

That depends upon what you really mean by “dynamically connect” and your “web application”.

If you want the data updated at periodic intervals, you can create your page to refresh, for example, every 5 seconds.

If you want this data to be updated whenever a value changes, you could use something like websockets with Channels to maintain a persistent connection with the server.

In either case, I would strongly encourage you to start small. That’s a very busy screen. I’d pick just one element (perhaps one that changes most frequently), and get my project working for that one element of the page before adding other elements.

---

<div class="post-metadata">

**Author:** ![Champ-Wei](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/champ-wei/32/12662_2.png) [@Champ-Wei](https://forum.djangoproject.com/u/Champ-Wei)\
**Post date:** [March 13, 2023, 2:47pm UTC](https://forum.djangoproject.com/t/how-to-use-django-to-search-database-and-display-info-in-web-application/19464/3 "2023-03-13T14:47:12Z")

</div>

Thank you very much, Ken. I shall and will check the Tutorial you mentioned.

By ‘dynamically connect’, I mean instead of writing the string manually:

> ‘\</div class=“main\_bottom\_t\_list\_title main\_bottom\_polity\_title1”\>北京神州绿盟科技有限公司\<//div\>’

I want it to get from the database through Django

> ‘\</div …\> {{ value }} \<//div\>’

If I use this:  
top\_5 = Isc.objects.raw("SELECT id, company, COUNT(\*) AS count FROM "  
"first\_project.cc\_statistic\_itsecchina "  
“GROUP BY company ORDER BY count DESC LIMIT 5”)

It returns a QuerySet. I’ve uploaded the picture of executing this SQL in MySQL workbench, it is actually a 2-D array.

But I cannot simply place [0][0] at the end of that set, what can I do in view.py?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 13, 2023, 2:53pm UTC](https://forum.djangoproject.com/t/how-to-use-django-to-search-database-and-display-info-in-web-application/19464/4 "2023-03-13T14:53:50Z")

</div>

You wrote earlier:

> [@Champ-Wei](#):
>
> I know a few things about creating classes in models.py, objects in views.py, and putting the value of objects in an HTML file.

> [@Champ-Wei](#):
>
> But I cannot simply place [0][0] at the end of that set, what can I do in view.py?

And so I repeat my earlier question.

> [@KenWhitesell](#):
>
> Have you actually worked your way thought either the [Official Django Tutorial](https://docs.djangoproject.com/en/4.0/intro/tutorial01/) or the [Django Girls Tutorial](https://tutorial.djangogirls.org/en/)? Those teach you how to build views containing data retrieved from the database.

So what did you learn from the tutorials about getting data from QuerySets?

Also, I do strongly recommend that you replace this:

> [@Champ-Wei](#):
>
> top\_5 = Isc.objects.raw("SELECT id, company, COUNT(\*) AS count FROM "  
> "first\_project.cc\_statistic\_itsecchina "  
> “GROUP BY company ORDER BY count DESC LIMIT 5”)

With an actual ORM query that uses your models. It generally makes things a whole lot easier.

---

<div class="post-metadata">

**Author:** ![Taswell11111](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/taswell11111/32/31353_2.png) [@Taswell11111](https://forum.djangoproject.com/u/Taswell11111)\
**Post date:** [January 25, 2026, 10:47pm UTC](https://forum.djangoproject.com/t/how-to-use-django-to-search-database-and-display-info-in-web-application/19464/5 "2026-01-25T22:47:11Z")

</div>

The design and layout is quite beautiful though…..
