Django framework

To use Django framework for our projects it is better to create one virtual environment wrapper , here is the command to create virtual environment for our Django project.
C:\Users\user_name>pip install virtualenvwrapper-win
This will create the environment but we have to do the setup.
C:\Users\user_name>mkvirtualenv my_dj
We used our environment as my_dj but you can use your own name to create your environment.

To create virtual environment. Move to the directory you want to create ( here it is smo_dj3)
C:\Users\user_name>cd smo_dj3
C:\Users\user_name\smo_dj3\>python -m venv my_dj
This will create one folder my_dj inside your smo_dj3 directory with sub - directories and files.
To activate the virtual environment. ( these files are created in above command )

If you have not installed Django then use this command
(my_dj) C:\Users\user_name>pip install django
(my_dj) C:\Users\user_name>cd smo_dj
Checking the version of Django
(my_dj) C:\Users\user_name\smo_dj>django-admin --version
Output 2.2.5. Same output we will get by using
(my_dj) C:\Users\user_name\smo_dj>python version
Note that we have installed Django in our virtual environment ( not in the full system ) . So it is only available to our my_dj environment.

Using help

python help
You will get a list of commands available.

Creating a project

(my_dj) C:\Users\user_name\django admin startproject my_proj
Now you can check your folder my_proj to see there are many files( and directory ) created by above command.

Starting web server.

Change to our project directory
(my_dj) C:\Users\user_name\cd my_proj
Now start the server ,
(my_dj) C:\Users\user_name\my_proj>python runserver
Open this URL at your browser
You can stop the server by using Ctrl + C
running server
Check system python check Check system

To start any app we will use this command
python startapp my_app

Using templates

Templates for adding content, let us create a directory smo_temp and we will set this inside file like this.
'DIRS': [os.path.join(BASE_DIR,'smo_temp')],
Your file should look like this.
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'smo_temp')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
We will create one home.html inside our smo_temp directory and let us keep this simple code inside it.
Welcome to

Inside my my_dir/ we can keep this code
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
    return render(request,'home.html')
The above code will display the text written inside home.html when we open URL

Publishing dynamic content

We can use our home.html file to publish dynamic content. Change the code inside home.html to this.
Welcome to  {{site}} to learn Python,<br>
 this is inside smo_temp, home.html file
Now let us change the file like this.
from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse

def index(request):
    return render(request,'home.html',{'site':'plus2net'})

django template language ( DTL )

We will create one template file temp_basic.html like this
<title>This is basic template file </title>
{% block content %}

{% endblock %}
<center>Displayed by using template file temp_basic.html</center>
This is the template we will use to display our dynamic content we created obove. Let us change the home.html file like this.
{% extends 'temp_basic.html' %}

{% block content %}

Welcome to {{site}}  to learn Python,<br>
 this is inside smo_temp, home.html file

 {% endblock %}
Now when we open our URL we will get the content through our template page. Here is the output
Welcome to plus2net to learn Python,
this is inside smo_temp, home.html file

Displayed by using template file temp_basic.html
Flow of a project and App

( One more Example )Creating Project

Navigate to the directory where we want to store our Project, let us give the name of the project as my_proj. I want to keep this project inside my D:\testing\ folder. My path should be like this. Note the virtual environment my_dj.
(my_dj) D:\testing>
From here I will start my project like this.
(my_dj) D:\testing>django-admin startproject my_proj
This will create one poject my_proj and subdirectory my_proj. It will also add files to these directories, here is the configuration.
(my_dj) D:\testing\my_proj\
my_proj The root or outer directory of the project. Command line utility to interact with Django Project my_proj/ : An empty file
my_proj/ : configuration of the project
my_proj/ : Table of contents of sites
my_proj/ : for ASGI compatible web servers
mysite/ : for WSGI compatible web servers
(my_dj) D:\testing\my_proj>python 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 migrate' to apply them.
January 06, 2022 - 15:20:35
Django version 4.0.1, using settings 'my_proj.settings'
Starting development server at
Quit the server with CTRL-BREAK.

Creating my_app application

To create one application ( my_app ), we will move to directory where our file is available.
(my_dj) D:\testing\my_proj>python startapp my_app
This will create my_app directory with some files. Here is the full list.
(my_dj) D:\testing\my_proj\

Creating view inside my_app

Inside my_app\ write this code. This is the simple text we will display as our first project.
from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
def index(request):
    return HttpResponse(" hello welcome to my_project")
Inside the same directory create ( my_app\ ) and enter this code.
from django.urls import path 
from  . import views

# Create your views here.
After this we will configure the my_proj/ to point to my_app.urls.
from django.contrib import admin
from django.urls import include,path

urlpatterns = [
    path('my_app/', include('my_app.urls')),
Now run the server
(my_dj) D:\testing\my_proj>python runserver
Open the path like this
Output is here
hello welcome to my_project

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer