📖 Scenario: You are starting a simple Django web project to manage a library's book collection. Understanding the project structure will help you organize your code and files properly.
🎯 Goal: Build the basic Django project structure with a project folder, an app folder, and essential files like settings.py, urls.py, and models.py.
📋 What You'll Learn
Create the main Django project folder named library_project
Inside library_project, create an app folder named books
Add the essential files: settings.py, urls.py in the project folder
Add models.py inside the books app folder
💡 Why This Matters
🌍 Real World
Django projects organize web applications into a main project and multiple apps, each handling specific features like managing books.
💼 Career
Understanding Django project structure is essential for web developers working with Django to build maintainable and scalable web applications.
Progress0 / 4 steps
1
Create the main Django project folder
Create a folder named library_project to hold your Django project files.
Django
Hint
Think of library_project as the main container for your Django project.
2
Add the app folder inside the project
Inside the library_project folder, create a folder named books to hold your app files.
Django
Hint
The books folder will contain the code related to managing books.
3
Add essential project files
Inside the library_project folder, create two files named settings.py and urls.py.
Django
Hint
settings.py holds configuration, and urls.py manages URL routing.
4
Add models.py inside the app folder
Inside the books folder, create a file named models.py to define your data models.
Django
Hint
models.py is where you define the structure of your data, like books.
Practice
(1/5)
1. Which file in a Django project is used to run commands like starting the server or creating migrations?
easy
A. manage.py
B. settings.py
C. urls.py
D. wsgi.py
Solution
Step 1: Identify the command runner file
The manage.py file is designed to run commands like starting the server or making migrations.
Step 2: Understand other files' roles
settings.py holds configuration, urls.py routes URLs, and wsgi.py is for deployment interface.
Final Answer:
manage.py -> Option A
Quick Check:
Command runner = manage.py [OK]
Hint: Remember: manage.py runs commands for your project [OK]
Common Mistakes:
Confusing settings.py as command runner
Thinking urls.py runs commands
Assuming wsgi.py runs server commands
2. Which folder typically contains the settings and URL configuration files in a Django project?
easy
A. The app folder
B. The templates folder
C. The project folder
D. The static folder
Solution
Step 1: Identify where settings.py and urls.py live
These files are inside the main project folder, which shares the project name.
Step 2: Differentiate folders
App folders contain app-specific code, templates hold HTML files, static holds CSS/JS/images.
Final Answer:
The project folder -> Option C
Quick Check:
Settings and URLs = project folder [OK]
Hint: Settings and URLs live in the project folder, not apps [OK]
Running manage.py from wrong folder usually gives different error
Forgetting imports in views.py causes different errors
5. You want to create a new Django app called shop inside your project. Which sequence of actions correctly follows Django project structure best practices?
hard
A. Run python manage.py startapp shop, then add shop to INSTALLED_APPS in settings.py
B. Create a folder named shop manually, then add shop to urls.py
C. Add shop to INSTALLED_APPS first, then run python manage.py startapp shop
D. Run python manage.py startproject shop inside your project folder
Solution
Step 1: Create the app properly
Use python manage.py startapp shop to create the app folder with correct files.
Step 2: Register the app in settings
Add shop to INSTALLED_APPS in settings.py so Django knows about it.
Final Answer:
Run startapp then add to INSTALLED_APPS -> Option A
Quick Check:
Start app then register in settings [OK]
Hint: Always start app with manage.py then add to INSTALLED_APPS [OK]