Creating Your First Django Project
# CHAPTER 4
Creating Your First Django Project
1. Introduction
With Python installed and our architectural blueprint understood, it is time to generate the actual code. Django provides a powerful command-line utility calleddjango-admin that automatically scaffolds the entire folder structure for a professional web application. In this chapter, we will generate a new project, explore the critical configuration files, and launch the built-in development server.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use
django-adminto scaffold a new Django project.
-
Understand the purpose of
manage.pyandsettings.py.
- Start the Django development server.
- Access the default Django welcome page in a web browser.
3. Beginner-Friendly Explanation
Imagine you are starting a new company. You don't want to build the office building brick by brick. You hire a construction company (django-admin) to instantly build the office for you.
They hand you the keys to the building. Inside, there is a master control room (manage.py) where you can turn the power on, run security checks, and update the database. There is also a rulebook (settings.py) that tells the office what language to speak, what timezone to use, and where the security cameras are located. You now have a fully functional office, ready for employees!
4. Step 1: Creating the Project
Ensure your terminal is open, and your virtual environment is active (you should see(env) in your terminal prompt).
Run the following command to tell Django to build your project. We will call our project core:
*(CRITICAL: Notice the space and the period . at the very end of the command! The period tells Django to build the files in your CURRENT folder, rather than creating an annoying nested folder structure).*
5. Step 2: Exploring the Generated Files
Typels (Mac/Linux) or dir (Windows) in your terminal. You will see a new file and a new folder:
Key Files Explained:
-
manage.py: A command-line utility that lets you interact with this Django project. You will use it constantly to start the server and update the database.
-
settings.py: Contains all the configuration for your Django installation (database connection, installed plugins, security keys, timezones).
-
urls.py: The main table of contents for your site. It routes URLs to specific views.
6. Step 3: Running the Development Server
Django comes with a built-in, lightweight web server so you can test your code locally without installing complex software like Apache or Nginx.In your terminal, run the master control file:
You should see output similar to this:
7. Step 4: Viewing Your Project
Open Google Chrome or Firefox. In the address bar, type:http://127.0.0.1:8000/ (or http://localhost:8000/).
You will see the iconic Django rocket ship taking off with the text: "The install worked successfully! Congratulations!"
*Note: You may see a red warning in your terminal about "unapplied migrations." Ignore this for now; we will solve it when we learn about databases in Chapter 8.*
8. Backend Workflow: Stopping and Restarting
The terminal window running your server is now "locked." You cannot type new commands into it. The server is actively listening for internet traffic.- To stop the server and regain control of your terminal, press Ctrl + C.
- Whenever you save a Python file, the development server will automatically restart itself to apply the new changes. (If you change HTML templates, you just refresh your browser).
9. Best Practices
-
The Secret Key: If you open
core/settings.py, you will see a massive string of random characters calledSECRET_KEY. This key is used to encrypt passwords and sessions. Never share this key. In production, this key must be hidden inside a.envfile, just like database passwords.
10. Common Mistakes
-
Forgetting the Period: If you run
django-admin startproject core(without the period at the end), Django creates a folder namedcore, and *inside* that folder, it creates another folder namedcore. This nestedcore/core/settings.pystructure confuses beginners endlessly when trying to import files. Always use the period!
11. Exercises
-
1.
Open
core/settings.py. Locate theTIME_ZONEvariable. Change it from'UTC'to your local timezone (e.g.,'America/New_York'or'Asia/Kolkata'). Save the file.
12. Coding Challenges
-
Challenge: Open your terminal, stop the server with
Ctrl + C. Then, usemanage.pyto start the server again, but this time, force it to run on port8080instead of the default8000. (Hint: Search online for "django runserver specific port").
13. MCQs with Answers
Which command is used to initially generate the core configuration files for a new Django project?
Which generated file acts as the primary command-line utility to interact with your specific Django project, allowing you to start the server and run database migrations?
14. Interview Questions
-
Q: Explain the purpose of the
settings.pyfile in a Django project. Name at least three critical configurations that are managed within this file.
- Q: What is the built-in Django development server? Why should it NEVER be used in a live production environment?
15. FAQs
Q: Can I renamemanage.py?
A: You technically could, but you shouldn't. It breaks conventions, and every Django tutorial on the internet assumes your file is named manage.py.
16. Summary
In Chapter 4, we transitioned from theory to practice. We utilized thedjango-admin command to scaffold a robust, professional project structure instantly. We explored the critical role of manage.py as our master control tool, and settings.py as our global configuration rulebook. Finally, we successfully launched the built-in development server and viewed our live application in the browser.