Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Django 4 for the Impatient

You're reading from   Django 4 for the Impatient Learn the core concepts of Python web development with Django in one weekend

Arrow left icon
Product type Paperback
Published in Jun 2022
Publisher Packt
ISBN-13 9781803245836
Length 190 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Greg Lim Greg Lim
Author Profile Icon Greg Lim
Greg Lim
Daniel Correa Daniel Correa
Author Profile Icon Daniel Correa
Daniel Correa
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Chapter 1: Installing Python and Django 2. Chapter 2: Understanding the Project Structure and Creating Our First App FREE CHAPTER 3. Chapter 3: Managing Django URLs 4. Chapter 4: Generating HTML Pages with Templates 5. Chapter 5: Working with Models 6. Chapter 6: Displaying Objects from Admin 7. Chapter 7: Understanding the Database 8. Chapter 8: Extending Base Templates 9. Chapter 9: Creating a Movie Detail Page 10. Chapter 10: Implementing User Signup and Login 11. Chapter 11: Letting Users Create, Read, Update, and Delete Movie Reviews 12. Chapter 12: Deploying the Application to the Cloud 13. Other Books You May Enjoy

Understanding the project structure

Let's look at the project files that were created for us in Chapter 1, Installing Python and Django, in the Installing Django section. Open the moviereviews project folder in VS Code. You will see the following elements:

Figure 2.1 – The MOVIEREVIEWS directory structure

Figure 2.1 – The MOVIEREVIEWS directory structure

Let's learn about each of these elements.

The moviereviews folder

As you can see in Figure 2.1, there is a folder with the same name as the folder we opened in VS Code originally – moviereviews. To avoid confusion and to distinguish between the two moviereviews folders, we will keep the inner moviereviews folder as it is and rename the outer folder moviereviewsproject.

After the renaming, open the inner moviereviews folder. You will see the following elements, as shown in Figure 2.2:

Figure 2.2 – The MOVIEREVIEWSPROJECT directory structure

Figure 2.2 – The MOVIEREVIEWSPROJECT directory structure

Let's briefly look at all the elements in the moviereviews folder:

  • __pycache__: This folder stores compiled bytecode when we generate our project. You can largely ignore this folder. Its purpose is to make your project start a little faster by caching the compiled code that can then be readily executed.
  • __init__.py: This file specifies what to run when Django launches for the first time.
  • asgi.py: This file allows an optional Asynchronous Server Gateway Interface (ASGI) to run.
  • settings.py: The settings.py file is an important file that controls our project's settings. It contains several properties:
    • BASE_DIR: Determines where on your machine the project is situated.
    • SECRET_KEY: Used when you have data flowing in and out of your website. Do not ever share this with others.
    • DEBUG: Our site can run in debug mode or not. In debug mode, we get detailed information on errors – for instance, if we try to run http://localhost:8000/123 in the browser, we will see a Page not found (404) error:
Figure 2.3 – Accessing an invalid application route

Figure 2.3 – Accessing an invalid application route

Note

It is important to remember the following:

  • When deploying our app to production, we should set DEBUG to False. If DEBUG = False, we will see a generic 404 page without error details.
  • While developing our project, we should set DEBUG = True to help us with debugging.
  • INSTALLED_APPS: Allows us to bring different pieces of code into our project. We will see this in action later.
  • MIDDLEWARE: Refers to built-in Django functions to process application requests/responses, which include authentication, session, and security.
  • ROOT_URLCONF: Specifies where our URLs are.
  • TEMPLATES: Defines the template engine class, the list of directories where the engine should look for template source files, and specific template settings.
  • AUTH_PASSWORD_VALIDATORS: Allow us to specify the validations that we want on passwords – for example, a minimum length.

There are some other properties in settings.py, such as LANGUAGE_CODE and TIME_ZONE, but we have focused on the more important properties in the preceding list. We will later revisit this file and see how relevant it is in developing our site.

  • urls.py: This file tells Django which pages to render in response to a browser or URL request. For example, when someone enters the http://localhost:8000/123 URL, the request comes into urls.py and gets routed to a page based on the paths specified there. We will later add paths to this file and better understand how it works.
  • Wsgi.py: This file stands for the Web Server Gateway Interface (WSGI) and helps Django serve our web pages. Both files are used when deploying our app. We will revisit them later when we deploy our app.

manage.py

The manage.py file seen in Figure 2.1 and Figure 2.2 is an element we should not tinker with. The file helps us to perform administrative operations. For example, we earlier ran the following command in Chapter 1, Installing Python and Django, in the Running the Django local web server section:

python3 manage.py runserver

The purpose of the command was to start the local web server. We will later illustrate more administrative functions, such as one for creating a new app – python3 manage.py startapp.

db.sqlite3

The db.sqlite3 file contains our database. However, we will not discuss this file in this chapter, as we do not need it to create our file. We will do so in Chapter 5, Working with Models.

Let's next create our first app!

You have been reading a chapter from
Django 4 for the Impatient
Published in: Jun 2022
Publisher: Packt
ISBN-13: 9781803245836
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 R$50/month. Cancel anytime