Django Apps and Project Structure
# CHAPTER 5
Django Apps and Project Structure
1. Introduction
Django has a unique and strict organizational philosophy regarding Projects versus Apps. This is often the most confusing concept for beginners. In Django, a "Project" is the entire website, while an "App" is a small, modular component that handles a specific feature (like a blog, a forum, or a store). In this chapter, we will generate our first Django App, integrate it into our Project, and explore the internal folder structure that powers the MVT architecture.2. Learning Objectives
By the end of this chapter, you will be able to:- Differentiate between a Django Project and a Django App.
-
Use
manage.py startappto create modular applications.
-
Register a new app within
settings.py.
-
Understand the roles of
views.py,models.py, andadmin.pywithin an App.
3. Beginner-Friendly Explanation
Imagine your Django Project is a Shopping Mall. The Mall itself (core/settings.py) provides the electricity, the security guards, and the main entrance doors.
However, a mall without stores is useless.
A Django App is a specific Store inside the mall.
-
You build an App called
shoesto sell shoes.
-
You build another App called
foodcourtto sell food.
shoes App goes bankrupt, you can easily remove it from the mall without affecting the foodcourt. This modularity means you can take your shoes App and plug it into a completely different Mall (Project) in the future!
4. The Golden Rule: Project vs. App
- A Project is the collection of configuration and apps for a particular website. (The Mall).
- An App is a Web application that does something (e.g., a Weblog system, a database of public records or a simple poll app). (The Store).
- *A project can contain multiple apps. An app can be in multiple projects.*
5. Step 1: Creating an App
Let's build a blog application. Ensure your virtual environment is active, and your terminal is in the same folder asmanage.py.
Run the following command:
If you type ls (or dir), you will see a new folder named blog has been created next to your core folder.
6. Step 2: Exploring the App Structure
Open the newblog folder. You will see several files:
*Notice that Django automatically created views.py and models.py for us! This is the MVT architecture in action.*
7. Step 3: Registering the App
You built the store (blog), but the Mall (core) doesn't know it exists yet. We must give the store a lease.
Open core/settings.py. Scroll down until you find a list named INSTALLED_APPS. Add your new 'blog' app to the bottom of the list.
*(Always add a comma after your app name!)*
8. Backend Workflow: When to create a new App?
How do you know when to create a new App? Ask yourself: "If I delete this feature, will the rest of the site still work?" If you are building an E-Commerce site:-
users(App for login/registration)
-
products(App for displaying items)
-
cart(App for checkout)
products/views.py gets to be 2,000 lines of code, it's much easier to manage than having a single file for the entire website.
9. Best Practices
-
App Naming Convention: App names should be plural, short, and descriptive nouns.
users,posts,products,invoices. Do not use verbs (create_post) and do not use generic words (main,app1).
10. Common Mistakes
-
Forgetting to Register the App: The #1 mistake beginners make is spending two hours building database models in
blog/models.py, running the database migration, and wondering why Django says "No changes detected." If the app is not listed inINSTALLED_APPS, Django will completely ignore the entire folder!
11. Exercises
- 1. Explain the "Shopping Mall" analogy. Why does Django encourage breaking a large website down into multiple smaller "Apps"?
12. Coding Challenges
-
Challenge: Using your terminal, create a second app named
accountsintended to handle user logins and registrations in the future. Once created, successfully register theaccountsapp inside yoursettings.pyfile.
13. MCQs with Answers
In Django terminology, what is the difference between a "Project" and an "App"?
After running python manage.py startapp blog, what is the mandatory next step to ensure Django recognizes the new application?
14. Interview Questions
- Q: Describe Django's modular App architecture. Provide a scenario where this modularity would allow a development team to reuse code across multiple entirely different company websites.
-
Q: Walk me through the files automatically generated by
startapp. What is the specific purpose ofmodels.pyandadmin.py?
15. FAQs
Q: Can I just build my whole website in one single App? A: Yes. If you are building a very simple, 3-page website, you can just create one app calledpages and put everything in there. But for enterprise applications, modularity is essential.
16. Summary
In Chapter 5, we learned the crucial distinction between a Django Project (the overall container) and a Django App (a modular feature). We usedmanage.py startapp to generate our blog application, explored its MVT-ready internal file structure, and performed the critical step of registering the app within settings.py's INSTALLED_APPS list. Our project is now wired and ready to receive code.