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

Creating our first app

A single Django project can contain one or more apps that work together to power a web application. Django uses the concept of projects and apps to keep code clean and readable.

For example, on a movie review site such as Rotten Tomatoes, as shown in Figure 2.4, we can have an app for listing movies, an app for listing news, an app for payments, an app for user authentication, and so on:

Figure 2.4 – The Rotten Tomatoes website

Figure 2.4 – The Rotten Tomatoes website

Apps in Django are like pieces of a website. You can create an entire website with one single app, but it is useful to break it up into different apps, each representing a clear function.

Our movie review site will begin with one app. We will later add more as we progress. To add an app, in the Terminal, stop the server by using Cmd + C. Navigate to the moviereviewsproject folder and run a command like the following in the Terminal:

python3 manage.py startapp <name of app>

In our case, we will add a movie app:

For macOS, run the following command:

python3 manage.py startapp movie

For Windows, run the following command:

python manage.py startapp movie

A new folder, movie, will be added to the project. As we progress in the book, we will explain the files that are inside the folder.

Although our new app exists in our Django project, Django doesn't recognize it till we explicitly add it. To do so, we need to specify it in settings.py. So, go to /moviereviews/settings.py, under INSTALLED_APPS, and you will see six built-in apps already there.

Add the app name, as highlighted in the following (this should be done whenever a new app is created):

…
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'movie',
]
…

Back in the Terminal, run the server:

For macOS, run with the following:

python3 manage.py runserver

For Windows, run with the following:

python manage.py runserver

The server should run without issues. We will learn more about apps throughout the course of this book.

Currently, you may notice a message in the Terminal when you run the server, as follows:

"You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them."

We will see how to address this problem later. But for now, remember that we can have one or more apps inside a project.

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 $19.99/month. Cancel anytime