Online store on Steemit blockchain| Step eighteen | Search system and output to the template.
Hello, friends!
Today we will create a search system for our site with courses, because without it it will be difficult for people to find some kind of course or some kind of information. In general, search is not an integral part of most sites. So today we are implementing it both on the backend and on the frontend. And for online stores, it will be generally a very useful thing.
Let's start by writing the path to our search. Through it, Django will understand which views to apply to him.
Also, do not forget to register imports. So that the path understands which views to access.
Important!
Imports and the path we are now registering in our project, and not in the application. But we take views from the market application.
Now let's write our views so that Django understands what exactly needs to be displayed and from which model information should be taken.
I don’t want to dwell on views much, since there is one point that young python developers do not pay attention to. Namely, the keyword. In our search option, there is a keyword that will be written in the html template. On request to which the search and further issuance of information will be carried out.
And already according to the standard scheme, we access our database in the product model through a get request. And we get our context.
Now let's take a look at our html template. There is a standard fronted as on the main page.
We carry out imports of the base page, as well as add a for loop through which we will display the information we need from our model. We also add content tags so that our django understands that this is the content section and displays it in the right place for us.
Now we need to mark up our search in the navbar so that it is below the directory button.
I think it is also worth considering our form in more detail. Since it's not just a div , it's a form, and of course, you need to add a CSRF token to it.
All requests are also implemented through get requests. And most importantly, returning to our views, we use the search keyword. Thus, Django understands what he needs to ask and from whom.
Now let's start our virtual server and see what we got!
Here we have such a beautiful search form. And now let's try it. We set the request for the word Python.
And now we can see the display for this keyword.
As we can see, the path goes first, then the CSRF token and its key, and only then comes the request itself.
Now let's make a request for the Mastering keyword.
As you can see everything works. Our search is fully functional.
Great!
Thanks for supporting the project.
@rme , @rex-sumon , @shy-fox , @hungry-griffin, @pennsif
The project is being created in partnership with @steemit-market.
Above may be wrong. Since I wrote the code from memory and then corrected it. But git will have no errors.
Link to the git.
You can get acquainted with the project here.
You can find all parts of the projects by the tag: #storeonsteemit
To be continued...
Sincerely, your HardPhotographer
Upvoted! Thank you for supporting witness @jswit.
TEAM 1
Congratulations! This post has been upvoted through steemcurator04. We support quality posts , good comments anywhere and any tags.Thank you very much @stef1 for your support.