Django Tutorial #2 - Setup & Installation
Table of Contents
Introduction
This tutorial will guide you through the setup and installation of Django, a powerful web framework for building web applications using Python. By the end of this guide, you'll have a Django project up and running, ready for development.
Step 1: Install Python
Before installing Django, ensure you have Python installed on your system. Follow these steps:
-
Check Python Installation
- Open a terminal or command prompt.
- Type
python --versionorpython3 --versionto check if Python is installed.
-
Download Python
- If Python is not installed, download it from the official website: python.org.
- Follow the installation instructions for your operating system (Windows, macOS, or Linux).
-
Add Python to PATH
- During installation on Windows, ensure you check the box to add Python to your PATH.
Step 2: Install Django
Once Python is installed, you can proceed with installing Django.
-
Open Terminal or Command Prompt
- Ensure you have access to a terminal or command prompt.
-
Install Django with pip
- Run the following command:
pip install django - This command will download and install the latest version of Django from the Python Package Index (PyPI).
- Run the following command:
-
Verify Django Installation
- After installation, check the version of Django by running:
python -m django --version - Ensure that it displays the installed version without errors.
- After installation, check the version of Django by running:
Step 3: Create a New Django Project
Now that Django is installed, you can create a new project.
-
Navigate to Your Development Directory
- Use the
cdcommand to navigate to the folder where you want your project:cd path/to/your/development/directory
- Use the
-
Start a New Django Project
- Run the following command to create a new project named "myproject":
django-admin startproject myproject
- Run the following command to create a new project named "myproject":
-
Navigate into Your Project
- Change into the project directory:
cd myproject
- Change into the project directory:
Step 4: Run the Development Server
To see your Django project in action, start the development server.
-
Run the Server
- Execute the command:
python manage.py runserver
- Execute the command:
-
Access the Development Server
- Open a web browser and go to
http://127.0.0.1:8000/. - You should see the Django welcome page, indicating that your project is set up correctly.
- Open a web browser and go to
Step 5: Explore Project Structure
Familiarize yourself with the structure of your new Django project.
-
Key Files and Directories
manage.py: A command-line utility for interacting with your project.settings.py: Configuration settings for your Django project.urls.py: URL routing for your application.wsgi.py: Entry point for WSGI-compatible web servers to serve your project.
-
Understanding the Structure
- Each of these files plays a crucial role in the functionality of your Django application.
Conclusion
You have successfully set up and installed Django, created a new project, and run the development server. From here, you can start building your web application by adding models, views, and templates.
Next Steps
- Explore the Django documentation: Django docs
- Begin learning about creating models and views to develop your application further.
- Consider accessing premium courses on platforms like Net Ninja Pro for in-depth learning.