Django - Create Virtual Environment
1. py -m venv soc-3
py: This refers to the Python launcher, often used to manage
multiple versions of Python installed on your system.
-m venv: This runs the venv module, which is responsible for
creating a virtual environment.
soc-3: This is the name of the virtual environment directory
that will be created.
2. What happens:
A new directory called soc-3 is created.
Inside this directory, Python will place its own executable files
and a copy of the pip tool, which you can use to install other
packages.
The virtual environment helps to isolate dependencies between
projects.
3. This will set up a virtual environment, and create a
folder named " soc-3 " with subfolders and files, like
this:
soc-3
Include
Lib
Scripts
pyvenv.cfg
4. To activate the virtual environment:
soc-3\Scripts\activate
Once activated, you can install packages using pip, and they will only
affect this virtual environment.
5. To exit a virtual environment like myworld, you simply
need to deactivate it.
deactivate
After running this, your terminal will return to its normal state, and
you will be outside of the virtual environment.
6. To remove the soc-3 virtual environment, simply delete
the directory that was created. You can do this using the
following command:
rmdir /s /q soc3
Install Django
py -m pip install Django
Check Django Version
You can check if Django is installed by asking for its version number
like this:
django-admin --version
5.1.1
Django Create Project
django-admin startproject cse_cst_club
Django creates a cse_cst_club folder on my computer, with this content:
cse_cst_club
manage.py
cse_cst_club /
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
Run the Django Project
py manage.py runserver
(soc-3) C:\Users\admin\cse_cst_club>py manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you
apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 13, 2024 - 00:03:41
Django version 5.1.1, using settings 'cse_cst_club.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[13/Sep/2024 00:03:54] "GET / HTTP/1.1" 200 12068
Not Found: /favicon.ico
[13/Sep/2024 00:03:54] "GET /favicon.ico HTTP/1.1" 404 2214
We have a Django project!
The next step is to make an app in your project.
You cannot have a web page created with Django without an app.
Django Create App
An app is a web application that has a specific meaning in your project,
like a home page, a contact form, or a members database.
But first, let's just create a simple Django app that displays "Hello
World!".
Create App
py manage.py startapp students
Django creates a folder named members in my project, with this content:
cse_cst_club
manage.py
cse_cst_club
students/
migrations/
__init__.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
First, take a look at the file called views.py.
This is where we gather the information we need to send back a proper
response.
Django Views
Django views are Python functions that take http requests and returns
http response, like HTML documents.
A web page that uses Django is full of views with different tasks and
missions.
Views are usually put in a file called views.py located on your app's
folder.
Find it and open it, and replace the content with this:
from django.shortcuts import render
from django.http import HttpResponse
def students(request):
return HttpResponse("Hello world!")
Django URLs
Create a file named urls.py in the same folder as the views.py file, and
type this code in it:
from django.urls import path
from . import views
urlpatterns = [
path('students/', views.students, name='students'),
]
The urls.py file you just created is specific for the students application.
We have to do some routing in the root directory cse_cst_club as well.
There is a file called urls.py on cse_cst_club folder, open that file and
add the include module in the import statement, and also add a path()
function in the urlpatterns[] list, with arguments that will route users
that comes in via 127.0.0.1:8000/.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('students.urls')),
path('admin/', admin.site.urls),
]
If the server is not running, navigate to the /cse_cst_club folder and
execute this command in the command prompt:
py manage.py runserver
In the browser window, type 127.0.0.1:8000/students/ in the address bar.