Django Search (with Q objects) Tutorial
Assuming you are implementing your django blog post with search form(Bootstrap) or any HTML forms, but do not know how make it functional. This post is created for you.
Prerequisites
YOU SHOULD
- Get familiar with basic Django, and its structures
- Get familiar with Bootstrap, and basic frontend programming languages , HTML, CSS.
How does search work ?(Brief)
We type any word e.g. “django” to our search form from a navbar section, then a client(Web browser) sends request to a server, a server makes query to a database. Based on a query, if found, response a post we previously made query to show on a web page
For instance, look at the images below captured from Django official website’s documentation
It has a search form
4 Steps closer
- We typed “admin” on this form to get desired admin results related to the word admin
- q (String field) is sent to a Django server with a value “admin”
- Django server receives q variable, and navigate to a database for seaching any posts related to, or contained with “admin”
- Then send posts back to a client showing all posts related to admin on a web page
Look at this url → search/?q=admin
While…
search: Location of search form indicated that search swill start executing after this url
? : A Separator or Indicator used to indicate that after this sign is a query string part
q: A field
=: Equals sign used to encapsulate value with q(string parametr)
admin: Value we typed to search for the result of “admin”
Note: This is a query string q=admin composed with field and value pair