Django framework

Django
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.

OR
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 )
C:\Users\user_name\smo_dj3>my_dj\Scripts\activate.bat


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 manage.py
(my_dj) C:\Users\user_name\smo_dj>python manage.py 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 manage.py 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 manage.py runserver
Open this URL at your browser
http://127.0.0.1:8000/
You can stop the server by using Ctrl + C
running server
Check system python manage.py check Check system

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

Using templates

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

views.py

Inside my my_dir/views.py 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 http://127.0.0.1:8000/my_dir/

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 view.py 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
<html>
<head>
<title>This is basic template file </title>
</head>
<body>
{% block content %}

{% endblock %}
<br><br>
<center>Displayed by using template file temp_basic.html</center>
</body>
</html>
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 http://127.0.0.1:8000/my_dir/ 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\manage.py
			  \my_proj\__init__.py
				  \asgi.py
				  \settings.py
				  \urls.py
				  \wsgi.py
my_proj The root or outer directory of the project.
manage.py Command line utility to interact with Django Project my_proj/__init__.py : An empty file
my_proj/settings.py : configuration of the project
my_proj/urls.py : Table of contents of sites
my_proj/asgi.py : for ASGI compatible web servers
mysite/wsgi.py : for WSGI compatible web servers
(my_dj) D:\testing\my_proj>python 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.
January 06, 2022 - 15:20:35
Django version 4.0.1, using settings 'my_proj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Creating my_app application

To create one application ( my_app ), we will move to directory where our manage.py file is available.
(my_dj) D:\testing\my_proj>python manage.py startapp my_app
This will create my_app directory with some files. Here is the full list.
(my_dj) D:\testing\my_proj\manage.py
			  \my_proj\__init__.py
				  \asgi.py
				  \settings.py
				  \urls.py
				  \wsgi.py
			   \my_app\migrations\__init__.py
				  \__init__.py
				  \admin.py
				  \apps.py
				  \models.py
				  \tests.py
				  \views.py

Creating view inside my_app

Inside my_app\views.py 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 urls.py ( my_app\urls.py ) and enter this code.
from django.urls import path 
from  . import views

# Create your views here.
urlpatterns=[
    path('',views.index,name='index'),
]
After this we will configure the my_proj/urls.py 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')),
    path('admin/', admin.site.urls),
]
Now run the server
(my_dj) D:\testing\my_proj>python manage.py runserver
Open the path like this
http://127.0.0.1:8000/my_app/
Output is here
hello welcome to my_project
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    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-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer