Setting Up Your Development Environment and Creating Your First Django Project
Introduction:
Developing web applications with Django requires a well-configured development environment. In this tutorial, we will walk through the steps of setting up your development environment and creating your first Django project. By the end of this guide, you’ll be ready to start building your own Django applications.
Prerequisites:
Before we begin, please ensure that you have Python installed on your machine. If you haven’t installed Python yet, you can download it from the official Python website (https://www.python.org/downloads/). For Windows users, open the command prompt (CMD), and for macOS/Linux users, open the terminal.
Step 1: Installing pipenv We’ll be using pipenv to manage Python dependencies and virtual environments. Run the following command in the command prompt or terminal:
For Windows:
pip install pipenv
For macOS/Linux:
pip3 install pipenv
Note: If you encounter a “pipenv command not found” error, follow the instructions in this article
(https://medium.com/@abdullahsohailcs/resolving-pipenv-command-not-found-error-in-python-54aff5652e80) to resolve the issue. You may need to add the location of pipenv to your environment variables.
Step 2: Setting up Visual Studio Code Download and install Visual Studio Code from the official website (https://code.visualstudio.com/download). Once installed, open Visual Studio Code and install the Python extension. This extension provides helpful features for Python development.
Step 3: Creating the Project Directory Navigate to your desired location using the command prompt or terminal. Use the cd
command to change directories, and the mkdir
command to create a new folder. For example:
cd Desktop
mkdir my_django_project
cd my_django_project
Step 4: Installing Django Inside your project directory, execute the following command to install Django using pipenv:
pipenv install django
This command will create a new virtual environment and install Django within it. Pipenv ensures that your project dependencies are isolated from other Python projects.
Step 5: Launching Visual Studio Code To start coding your Django project, open Visual Studio Code within the project directory. You can either use the command code .
in the command prompt or terminal, or open Visual Studio Code manually and select the project folder.
Step 6: Activating the Virtual Environment To activate the virtual environment created by pipenv, run the following command in the command prompt or terminal:
pipenv shell
This command will activate the virtual environment and ensure that the Python interpreter used is specific to your project.
Step 7: Initializing the Django Project With the virtual environment activated, we can now initialize our Django project. Run the following command:
django-admin startproject my_project .
This command creates a new Django project named “my_project” in the current directory. The dot at the end ensures that the project files are placed in the current directory instead of creating a subdirectory.
Step 8: Running the Django Development Server To start the Django development server, navigate into the project folder if you’re not already there, and run the following command:
python manage.py runserver
By default, the development server runs on port 8000. You can specify a different port number (e.g., 9000) if necessary. Once the server starts, you’ll see an IP address printed in the console.
Step 9: Accessing Your Django Application Copy the IP address shown in your console and paste it into your web browser. You should see the default Django welcome page, indicating that your application is running successfully.
Conclusion:
Congratulations! You have successfully set up your development environment and created your first Django project. Now you’re ready to dive into Django’s powerful framework and start building amazing web applications. Remember to explore the Django documentation and other resources to unleash the full potential of this web framework. Happy coding!