Book Image

Web Development with Django - Second Edition

By : Ben Shaw, Saurabh Badhwar, Chris Guest, Bharath Chandra K S
4.7 (3)
Book Image

Web Development with Django - Second Edition

4.7 (3)
By: Ben Shaw, Saurabh Badhwar, Chris Guest, Bharath Chandra K S

Overview of this book

Do you want to develop reliable and secure applications that stand out from the crowd without spending hours on boilerplate code? You’ve made the right choice trusting the Django framework, and this book will tell you why. Often referred to as a “batteries included” web development framework, Django comes with all the core features needed to build a standalone application. Web Development with Django will take you through all the essential concepts and help you explore its power to build real-world applications using Python. Throughout the book, you’ll get the grips with the major features of Django by building a website called Bookr – a repository for book reviews. This end-to-end case study is split into a series of bitesize projects presented as exercises and activities, allowing you to challenge yourself in an enjoyable and attainable way. As you advance, you'll acquire various practical skills, including how to serve static files to add CSS, JavaScript, and images to your application, how to implement forms to accept user input, and how to manage sessions to ensure a reliable user experience. You’ll cover everyday tasks that are part of the development cycle of a real-world web application. By the end of this Django book, you'll have the skills and confidence to creatively develop and deploy your own projects.
Table of Contents (19 chapters)

Populating the Bookr project’s database

Although we know how to create database records for the project, in the next few chapters, we will have to create a lot of records to work with the project. Therefore, we have created a script that can make things easy for us. This script populates the database by reading a comma-separated values (CSV) file consisting of many records. Follow the next few steps to populate the project’s database:

  1. Create the following folder structure inside the project directory:
    bookr/reviews/management/commands/
  2. Copy the loadcsv.py file from the following location and WebDevWithDjangoData.csv into the folder created. This can be found on the GitHub repository for this book at http://packt.live/3pvbCLM.

Because loadcsv.py is placed inside the management/commands folder, it now works like a Django custom management command. You can go through the loadcsv.py file and read more about writing Django custom management commands here: https://docs.djangoproject.com/en/3.0/howto/custom-management-commands/.

  1. Now, let’s recreate a fresh database. Delete the SQL database file present in the project folder:
    rm db.sqlite3
  2. To create a fresh database, execute the Django migrate command:
    python manage.py migrate

Now you can see the newly created db.sqlite3 file under the bookr folder.

  1. Execute the loadcsv custom management command to populate the database:
    python manage.py loadcsv --csv reviews/management/commands/WebDevWithDjangoData.csv
  2. Using DB Browser for SQLite, verify that all the tables created by the bookr project have been populated.

With this, we have successfully populated our application’s database, which will come in handy to work with in the upcoming chapters of this book.