Creating Your First Django Project
Lesson 3: Creating Your First Django Project
In this lesson, we will walk through the process of creating your first Django project and understanding its structure. Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.
Creating a New Django Project
To create a new Django project, you will use the django-admin command-line utility. This utility helps you to create a new project and manage various aspects of it.
Step 1: Create a New Project
Open your terminal and navigate to the directory where you want to create your project. Run the following command:
django-admin startproject myproject
Replace myproject with your desired project name. This command creates a new directory named myproject with the following structure:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
Project Structure Explained
- manage.py: A command-line utility that lets you interact with your project. You can run development servers, migrate databases, and more.
- myproject/: This is the inner directory that contains your project settings.
__init__.py: An empty file that tells Python to treat the directory as a package.- settings.py: Contains all the configuration for your Django project. You will define database settings, static files, and more here.
- urls.py: The URL declarations for your project. This is where you define the mapping of URLs to views.
- wsgi.py: An entry point for WSGI-compatible web servers to serve your project.
Step 2: Run the Development Server
Now that you have created your project, you can run the development server to see if everything is set up correctly. Navigate into your project directory and run:
cd myproject
python manage.py runserver
You should see output indicating that the server is running. Open your web browser and go to http://127.0.0.1:8000/. You should see the Django welcome page!
Best Practices
- Keep your project organized: As your project grows, consider organizing your code into separate apps.
- Use virtual environments: Always develop your projects in a virtual environment to avoid dependency conflicts.
Common Mistakes
Ensure that Django is installed: If you encounter an error like
django-admin: command not found, make sure Django is installed in your environment.Running the server from the wrong directory: Always run
manage.pyfrom the project's root directory.
Summary
In this lesson, you learned how to create a new Django project and explored its structure. You also learned how to run the development server to check your setup. This foundational knowledge is crucial as you continue to build your Django applications.
Exercises
- Exercise 1: Create a new Django project named
myfirstsite. Run the development server and check if the welcome page appears. - Exercise 2: Modify the
settings.pyfile to change theDEBUGsetting toFalse. What happens when you try to run the server? - Exercise 3: Add a new app to your project using
python manage.py startapp blog. What files are created in theblogdirectory?
Summary
- You can create a new Django project using
django-admin startproject. - The project structure includes important files like
manage.py,settings.py, andurls.py. - Running the development server is crucial for testing your setup.
- Always adhere to best practices like using virtual environments and keeping your project organized.