Django is a powerful web framework written in Python that allows for rapid development of web applications. In this tutorial, we will walk you through the process of creating a Django project using Visual Studio Code, setting up a virtual environment, and creating a home page for your project.
Step 1: Install Python and Visual Studio Code
Before we can begin creating our Django project, you will need to have Python and Visual Studio Code installed on your machine. You can download Python from the official website (https://www.python.org/) and Visual Studio Code from the Microsoft website (https://code.visualstudio.com/).
Step 2: Install the Django framework
Once you have Python and Visual Studio Code installed, you can install the Django framework by opening a command prompt and running the following command:
pip install django
This will download and install the Django framework on your machine.
Step 3: Create a new Django project
Now that we have Django installed, we can create a new Django project by opening Visual Studio Code and following these steps:
- Open Visual Studio Code and create a new folder for your Django project.
- Open the command prompt in Visual Studio Code by pressing
Ctrl + ~
. - Navigate to the folder you created by using the
cd
command. For example, if you created a folder namedmyproject
, you would typecd myproject
. - Once you are in the folder, run the following command to create a new Django project:
django-admin startproject myproject
This will create a new Django project named myproject
in the folder you created.
Step 4: Create a virtual environment
It is recommended to use a virtual environment when working on Django projects to isolate your project dependencies from other projects on your machine. You can create a virtual environment by running the following command in the command prompt:
python -m venv env
This will create a new virtual environment named env
in your project folder.
Step 5: Activate the virtual environment
After creating the virtual environment, you will need to activate it by running the following command in the command prompt:
Windows:
.envScriptsactivate
Mac/Linux:
source env/bin/activate
You will know the virtual environment is activated when you see (env)
at the beginning of your command prompt.
Step 6: Install Django in the virtual environment
Now that you have activated the virtual environment, you can install Django in the virtual environment by running the following command:
pip install django
This will install Django in the virtual environment.
Step 7: Run the Django development server
Once you have Django installed in the virtual environment, you can run the Django development server by running the following command in the command prompt:
python manage.py runserver
This will start the Django development server, which you can access in your web browser by navigating to http://127.0.0.1:8000/
.
Step 8: Create a home page for your Django project
To create a home page for your Django project, you will need to create a new app within your Django project. You can create a new app by running the following command in the command prompt:
python manage.py startapp home
This will create a new app named home
within your Django project.
Next, you will need to create a view for your home page. Open the views.py
file within the home
app and add the following code:
from django.shortcuts import render
def home(request):
return render(request, 'home/index.html')
This code defines a view function named home
that renders the index.html
template located in the templates/home
directory.
Step 9: Create a template for the home page
Next, you will need to create a templates
directory in your project folder. Inside the templates
directory, create a new directory named home
. Within the home
directory, create a new HTML file named index.html
and add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>Welcome to my Django project!</h1>
</body>
</html>
Step 10: Connect the view to a URL
Finally, you will need to connect the view you created to a URL. Open the urls.py
file within the myproject
directory and add the following code:
from django.urls import path
from home import views
urlpatterns = [
path('', views.home, name='home'),
]
This code defines a URL pattern that maps the home view to the root URL of your Django project.
Step 11: Run the development server
After configuring the view, template, and URL for the home page, you can run the development server again by running the following command in the command prompt:
python manage.py runserver
You can now access the home page of your Django project in your web browser by navigating to http://127.0.0.1:8000/
.
Congratulations! You have successfully created a Django project in Visual Studio Code, set up a virtual environment, and created a home page for your project.
Sir while entering the password keys are not working what can I do for this
i love u so much
Very useful video
Bro… Ur are god to me… I was stuck with problems for 3days.. I got many problems while creating startapp part.. while starting of ur video I thought" this is the last try.. if it didn't work I quiet learning"… U saved my life and learning 🥺.. thanks bro
I have also written it the same way as you wrote, but when last you wrote python manage.py server in gitbash, it is not there in me and what should I do in showing it now
Who knew a non-verbal video would be one of the most helpful tutorials on youtube lol
Amazing
I did like in the video, step by step and I can't get the code to runserver again. Later: had to Ctrl+C to stop the server
I have error because source is not recognized as an internal or external command it showing me 😢pls anyone reply me plsssssssssss plsssssssssss plsssssssssss plsssssssssss plsssssssssss😢😢😢😢 iam doing this setup for 2days but i can't find that😢😢😢
Thank you so much for the video…..😊😊
At last terminal block ho Gaya input hi nahi le raha
I am not found git bash
you are THE GOAT!!!! zkn i love you man
Easy to understand
God sent
Big Thanks !
The 10 minutes of your time just saved lots of people's lives 🙏🏽🤜🏽🤛🏽
Great!!!!
you are great easiest to understand and easy to follow for begginers
thank youu