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

I’m currently working on a data visualization board.


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:

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

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. : )

Have you actually worked your way thought either the Official Django Tutorial or the Django Girls Tutorial? Those teach you how to build views containing data retrieved from the database.

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.

1 Like

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?

You wrote earlier:

And so I repeat my earlier question.

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

Also, I do strongly recommend that you replace this:

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

1 Like