Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Becoming an Enterprise Django Developer

You're reading from  Becoming an Enterprise Django Developer

Product type Book
Published in Jun 2022
Publisher Packt
ISBN-13 9781801073639
Pages 526 pages
Edition 1st Edition
Languages
Author (1):
Michael Dinder Michael Dinder
Profile icon Michael Dinder

Table of Contents (15) Chapters

Preface 1. Part 1 – Starting a Project
2. Chapter 1: Undertaking a Colossal Project 3. Chapter 2: Project Configuration 4. Chapter 3: Models, Relations, and Inheritance 5. Part 2 – Django Components
6. Chapter 4: URLs, Views, and Templates 7. Chapter 5: Django Forms 8. Chapter 6: Exploring the Django Admin Site 9. Chapter 7: Working with Messages, Email Notifications, and PDF Reports 10. Part 3 – Advanced Django Components
11. Chapter 8: Working with the Django REST Framework 12. Chapter 9: Django Testing 13. Chapter 10: Database Management 14. Other Books You May Enjoy

Chapter 2: Project Configuration

Source code is considered the meat and bones, or the framing of a home, in any software. In this chapter, we will build a project that contains the files where the source code lives. We will discuss several tools that will come in handy when developers are working directly with their source code. When working with Django, while any tool can be used to edit the source code, some tools are more productive than others. In this chapter, we will explore some of the countless tools that exist and discuss why an Integrated Development Environment (IDE) might also be used.

We'll also learn about the importance of working with the Django settings.py file(s) of a project. Of course, software also requires a database to store and retrieve data entered and created by its users, and we will install a local and remote database for each environment of a project. We will go over the various database types that are available and then focus on the most popular...

Technical requirements

To work with the code in this chapter, the following tools will need to be installed on your local machine:

  • Python version 3.9 – used as the underlying programming language for the project
  • Django version 4.0 – used as the backend framework of the project
  • pip package manager – used to manage third-party Python/Django packages

Next, you will need a way to edit the code that we will be writing in this chapter and throughout the rest of this book. The first section of this chapter will provide you with several development tool choices from text editors to IDEs. The same IDE will be used to demonstrate a few actions but it is not necessary to use the same tool. You are welcome to use any IDE you like or use no IDE at all, using the terminal or command-line window instead. A database will also be required, and the third section of this chapter will provide several options to choose from. Any database type will work with...

Choosing development tools

Configuring our project refers to how we will structure and arrange the files that make up an application. This also refers to how we share those files within a collaborative team. Some tools create files that can be shared among your team members, such as preconfigured settings related to development and debugging features. These files are sometimes referred to as configuration or solution files. This means you can preconfigure a set of development tools to help get your team up to speed quickly. Making sure that all members use similar tools makes debugging and looking at code that is not written by you much easier. This consistency also makes verbal and written communication among members of your team more efficient when synchronizing workflows.

While there are benefits to sharing project configuration files, it is also not necessary for everyone on your team to be using the same tools. In fact, it's even possible to create many different configuration...

Starting a project

There are two ways to start a project and this chapter will allow you to choose which method you want to follow. We encourage you to use the IDE as becoming proficient with using this tool in your team will be beneficial in the long run. However, if your team is using an IDE other than Visual Studio or you are only using a text editor to work with your code, the command-line equivalent of performing each step is also provided to allow anyone to work through this chapter. All other chapters in this book will focus on code only, which can be used with or without an IDE.

Using the IDE

Open the Visual Studio IDE and select Create New Project. When you are directed to the next screen, search for the django keyword and in the list of results, select Blank Django Web Project, as depicted here:

Figure 2.2 – Visual Studio – Create a new project

On the next screen, enter becoming_a_django_entdev for Project Name. This will auto...

Creating a virtual environment

We should not have a virtual environment for our project at this time. If you do have one, go ahead and disregard it and create a new one for this next exercise. Whether you created a project using the Visual Studio IDE or by using the Django commands from a terminal or command-line window in the previous exercises, the file structure in your repository should look like the following tree structure:

├── .git

├── readme.md

├── requirements.txt

├── becoming_a_django_entdev

│ ├── .vs

│ ├── becoming_a_django_entdev.sln

│ ├── db.sqlite3

│ ├── manage.py

│ ├── obj

│ ├── requirements.txt

│ ├── staticfiles

│ └──...

Project configuration

All projects need configuring in some way to work with all of the packages and hosts that are involved. We will take the project that we just created and configure files such as settings.py, .env, procfile, and .gitignore. Take the settings.py file—this file or files will store all of the global constants that are used throughout the code of your project. When packages are used, they usually provide a way to customize the behavior of that package from within the main settings file. Other files, such as .env and procfile, will be used to prevent deployment issues when working with Heroku as the host. Currently, we would not be able to deploy successfully to the Heroku environment as we have created the project files. Work through the following configuration sections before attempting a successful deployment.

Django settings.py file

In the settings.py file that was automatically generated for us when we created a project, we need to add settings specific...

Using basic database settings

A website by itself is far from useful without a database to talk to; this is why the Visual Studio IDE comes with a lightweight and portable SQLite3 database. Visual Studio will create a file called db.sqlite3 in the same folder as your manage.py file whenever the startproject command is executed. If you created your project using the terminal or command-line window, then you will not have a SQLite database that is used in the following configuration examples, and if you attempt to run your project without this database, it will fail. This is one of the five standard database types that Django directly supports. Database types other than the five types that Django directly supports can also be used. We will also provide an example of how to configure other types of databases, such as the Microsoft SQL Server database. Types that are not any of the five standard types will require using a different engine than the engines that Django provides, which means...

Preparing PostgreSQL for Heroku

This section is dedicated to actually configuring each of your environments, whether using an IDE or command-line window or terminal. To install and use PostgreSQL, we will need to install it locally and then again in each of our remote environments.

Installing PostgreSQL

This section will guide you through installing the PostgreSQL software needed on your local and remote machines.

Local installation

To use PostgreSQL, we will need to install a suite of software and drivers on each of our machines. In the Heroku dashboards, we will need to include add-ons to get PostgreSQL installed in those environments. To install it on your development machine, you can choose to download the installer for your platform directly from the publisher's website, found here: https://www.postgresql.org/download/. During installation, make note of the port that you set, the password that you create, and whether it asks you to add anything to your environment...

Summary

By now, we have done a lot of work but we still haven't really begun building any Django apps for a project yet. All of the work done up until now can be thought of as the preliminary work necessary before handing over a project to the development team. Two methods of creating a project have been provided: one method using a tool to help streamline production, called an IDE, and another method using commands in a terminal or command-line window. We are tracking a solution file in the repository so that we can share it within a team but we are not tracking personal settings and debug files that are automatically created when running a project. Developers who are not using an IDE can still work with the code base even when sharing project configuration files with those who are using an IDE. After we did that, we configured Django to work with the host provider Heroku on both the project level and the database level. Finally, we activated tools that allow developers to view...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Becoming an Enterprise Django Developer
Published in: Jun 2022 Publisher: Packt ISBN-13: 9781801073639
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}