Creating Your First Django Project
Learning Objectives
By the end of this lesson, you will be able to: - Create a new Django project using the command line. - Understand the structure of a Django project and its components. - Recognize the purpose of each file and directory in a Django project. - Run your Django project and access it through a web browser.
Introduction to Django Projects
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. When you create a Django project, you are essentially creating a container for your web application that includes various configurations, settings, and components necessary for web development.
Step 1: Creating a New Django Project
To create a new Django project, you will use the django-admin command-line tool that comes with Django. Before proceeding, ensure that you have Django installed in your development environment. If you followed the previous lesson, you should already have it installed. If not, you can install it using pip:
pip install django
Now, to create your first Django project, open your command line interface (CLI) and run the following command:
django-admin startproject my_first_project
In this command, my_first_project is the name of your project. You can choose any name you like, but it's a good practice to use lowercase letters and underscores.
Step 2: Understanding the Project Structure
Once you execute the command, Django will create a new directory named my_first_project containing the following files and directories:
my_first_project/
manage.py
my_first_project/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Let’s break down what each of these components does:
-
manage.py: This is a command-line utility that lets you interact with your project. You can use it to run the development server, create database migrations, and perform other administrative tasks. -
my_first_project/: This is the inner directory that contains the actual project package. It shares the same name as your project and contains configuration files. -
__init__.py: This file indicates that the directory should be treated as a Python package. It can be empty or contain initialization code for the package. -
settings.py: This file contains all the configuration settings for your Django project, including database settings, static files configuration, and middleware settings. This is where you will spend a lot of time customizing your project. -
urls.py: This file is responsible for routing URLs to the appropriate views. It essentially maps web addresses to Python functions that handle the requests. -
asgi.py: This file is used for asynchronous server gateways. It is useful for deploying your application in an asynchronous environment. -
wsgi.py: This file is used for deploying your application in a synchronous environment. It acts as the entry point for WSGI-compatible web servers to serve your project.
Step 3: Running the Development Server
Now that you have created your Django project and understand its structure, it's time to run the development server. This server is built into Django and is perfect for testing and development purposes. To start the server, navigate to your project directory in the command line:
cd my_first_project
Then, run the following command:
python manage.py runserver
You should see output indicating that the server is running, along with a message similar to this:
Starting development server at http://127.0.0.1:8000/
Step 4: Accessing Your Project in a Web Browser
Open your web browser and navigate to the URL provided in the terminal (http://127.0.0.1:8000/). You should see a welcome page that confirms your Django project is up and running:
The install worked successfully! Congratulations!
Common Mistakes and How to Avoid Them
- Not Activating the Virtual Environment: Always ensure your virtual environment is activated before running Django commands. If you installed Django in a virtual environment, use the following command to activate it:
bash
source venv/bin/activate # On macOS/Linux
.\venv\Scripts\activate # On Windows
-
Using the Wrong Python Version: Ensure you are using the correct version of Python that is compatible with Django. Django 3.x and later require Python 3.6 or later.
-
Incorrect Project Name: Avoid using spaces or special characters in your project name. Stick to lowercase letters and underscores.
Best Practices
- Use Version Control: Initialize a Git repository in your project directory to keep track of changes and collaborate with others.
- Organize Your Code: As your project grows, consider organizing your code into separate apps within the project. This will help maintain a clean directory structure.
- Keep Your Settings Secure: Do not hard-code sensitive information (like API keys) in your
settings.pyfile. Use environment variables instead.
Key Takeaways
- You have successfully created your first Django project and understood its structure.
- The key components of a Django project include
manage.py,settings.py,urls.py,wsgi.py, andasgi.py. - You can run your Django project using the built-in development server and access it via a web browser.
Next Steps
In the next lesson, titled "Understanding Django Apps," we will explore the concept of Django apps, how they fit into your project, and how to create and manage them effectively. This understanding will be crucial as you start building more complex web applications with Django.
Exercises
- Exercise 1: Create a new Django project named
my_second_projectand explore its directory structure. - Exercise 2: Modify your
settings.pyfile to change theTIME_ZONEsetting to your local time zone. - Exercise 3: Add a new URL pattern in
urls.pythat points to a view you will create in the next lesson. - Exercise 4: Run the development server and try accessing the URL you added in the previous exercise.
- Practical Assignment: Create a Django project named
personal_blog, set up the project structure, and prepare it for future development by configuring the settings and adding a simple URL route.
Summary
- You learned how to create a new Django project using the
django-admincommand. - The structure of a Django project includes important files like
manage.py,settings.py, andurls.py. - You can run your project using the built-in development server and access it through a web browser.
- Common mistakes include not activating the virtual environment and using incorrect project names.
- Best practices include using version control and keeping sensitive settings secure.