Getting started with Django can be a bit tricky for beginners, but with a step-by-step approach, you can quickly create your first Django project and add it to a GitHub repository. In this article, we'll go through the process of creating a Python environment, installing Django, starting a Django project, and creating a repository for the project.
Step 1: Move to the Directory Firstly, open your command prompt or terminal and move to the directory where you want to create a Django project.
Step 2: Create Virtual Environment The next step is to create a virtual environment. A virtual environment is a tool that allows you to create an isolated Python environment for your project. It helps you manage different versions of packages for different projects. To create a virtual environment, run the following command:
py -m venv env
Once you've created the virtual environment, activate it using the following command:
env\Scripts\activate
Step 3: Install Django After activating the virtual environment, the next step is to install Django using pip. You can install Django by running the following command:
pip install django
Step 4: Create a Django Project Now that you have Django installed, you can create a Django project using the django-admin
command. Run the following command to create a Django project:
django-admin startproject [project_name] .
This command creates a new directory with your project name and all the necessary files for a Django project.
Step 5: Run the Project To run the Django project, execute the following command:
py manage.py runserver
This command will start the development server and make the application available at http://127.0.0.1:8000/
.
Step 6: Create .gitignore File To create a repository for your project, you'll need to create a .gitignore
file at the root directory of your project. This file tells Git which files to ignore when you push your project to a repository. You can create a .gitignore
file by running the following command:
echo "venv/" > .gitignore
Step 7: Initiate the Git Repo The next step is to initiate a Git repository for your project. Run the following command to initialize a Git repository:
git init
Step 8: Add Files to Repo After initializing the Git repository, you can add all the files in the project to the repository using the following command:
git add .
Step 9: Make First Commit Once you've added all the files to the repository, you can make your first commit. To do this, run the following command:
git commit -m "Initial commit"
Step 10: Create a New Repo on GitHub The next step is to create a new repository on GitHub. Once you've created the repository, copy the repository link.
Step 11: Push Your Code to GitHub Now, you can push your code to GitHub by executing the following commands:
git remote add origin [repo link]
git branch -M main
git push -u origin main
Once you've executed these commands, you can refresh your GitHub repository page and see your code in the repository.