Chapter 6. Daintree – an E-commerce Site

We have created some solid web applications in the previous chapters. They were simple, but had enough functionality to be used in real-world projects. With some frontend work, our applications could very well be deployed on the Internet and solve real problems. Now it's time to look at something more complicated.

I'm sure you have used, or at least heard of, some of the big names in the e-commerce space—names such as Amazon and Ali Baba. While these sites are very complicated beasts, a basic e-commerce site is pretty simple under the hood. E-commerce sites are also something that a lot of clients want created, so having some knowledge of how to make a good one will be very useful in your career.

A basic e-commerce site has one main purpose: to help users find and buy products from the online store. Django alone can be used to build an e-commerce site quickly, using database queries to allow searches across product range, but this doesn't scale well. Databases are designed to quickly save and retrieve rows of data, but they are not optimized to search across the entire dataset (or a subset). Once the traffic of your site starts to increase, you'll see the search speed go down very quickly. On top of that, there are some features that are very difficult to build with a database.

Instead, we will use a search server. A search server is very much like a database. You give it some data to store and then you can retrieve it later. It also has features specifically built to help you add searching to your applications. You might wonder that if a search server can store our data like a database, then can we not just get rid of the database? We can, but usually it's not recommended. Why? Well, because the search server is designed for a different use case. While it can store your data, a database provides a lot of guarantees about the storage that a search server usually doesn't. For example, a good database (such as MySQL or PostgreSQL) gives you a guarantee that if you try to save something and the database returns a successful response, your data will not be lost in case of a crash or power outage or some other problem. This is called durability. A search server does not provide this guarantee because that's not what they are designed for. It is usually a good idea to keep our data in a database and use the search server just to search across our data.

For the application that we will develop in this chapter, we will be using Elasticsearch, one of the most popular and perhaps easy-to-use search servers available. It's also open source and available for free. So let's get started. This is going to be an exciting chapter!

Code pack

The code pack for this chapter contains a basic web application that has the models and views for a simple e-commerce site. Right now there is no search, just a page that lists all the available products. I have also provided a data dump that contains about 1,000 products so that our database has some data that we can play around with. As always, download the code pack, create a new virtual environment, install Django, run the migrate command, and then issue the run server command to start the development server. You should have the hang of how to do these things without any guidance by now.

To load the test data, run the following command after the migrate command:

> python manage.py loaddata main/fixtures/initial.json

This should fill your database with a thousand sample products and give us enough data to play around with.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.184.90