Django Virtual Environment
• A virtual environment is a self-contained directory that contains a
Python installation for a particular version of Python, plus several
additional packages.
Setting up Virtual Environment
1. Ensure you have Python installed.
2. While Python 3.3+ includes the venv module, you can also install
virtualenv, which offers more features. To install it, run:
pip install virtualenv
3. Navigate to your project directory in your terminal or command
prompt and create a virtual environment. Using venv:
python -m venv myenv Or virtualenv myenv
4. Activate the Virtual Environment on Windows:
myenv\Scripts\activate
5. With the virtual environment activated, install Django:
pip install django
6. To start a new Django project, run:
django-admin startproject myproject
This will create a new directory named myproject with the necessary
Django files.
7. Running the Development Server - Navigate into your project
directory: cd myproject Then, run the development server:
python [Link] runserver
8. You can access your project in your web browser at
[Link]
Text Editors
• choosing a text editor or IDE for Django development, you have several
options.
Notepad++
• Lightweight and simple text editor. Basic syntax highlighting for Python.
Visual Studio Code (VS Code)
• Lightweight, highly customizable, and has a vast library of extensions.
Jupyter Notebook
• Interactive notebooks for data science and experimentation.
PyCharm
• Comprehensive IDE specifically designed for Python development.
Hello World App
• Creating a "Hello World" app in Django involves setting up a project,
creating an app, and defining views and URL configurations.
Step 1: Initial Setup
Create a Virtual Environment
Install Django
Create a Django Project
Navigate into your project directory
Step 2: Create an App
Run the following command to create a new app called hello
python [Link] startapp hello
This creates a new directory hello with the necessary files.
Step 3: Define Views - Create a Simple View
Open hello/[Link] and add the following code:
from [Link] import HttpResponse
def hello_world(request):
return HttpResponse("Hello, World!")
Step 4: (1) Configure URLs - Set Up URL Configuration
In your app (hello): Create a file called [Link] inside the hello directory and add
the following code:
from [Link] import path
from .views import hello_world
urlpatterns = [
path('', hello_world, name='hello_world'),
]
(2) Include App URLs in the Project
Now, open the [Link] file in the myproject directory (created in Step 1) and modify
it to include the app’s URLs:
from [Link] import admin
from [Link] import path, include # include is imported
urlpatterns = [
path('admin/', [Link]),
path('hello/', include('[Link]')), # Include the hello app URLs
]
Step 5: Run the Development Server
1. Start the Server
In your terminal, run the following command:
python [Link] runserver
2. Access the App
Open your web browser and navigate to [Link] You should
see the message:
Hello, World!
Pages app