How to install Django python framework in windows 10

We know Django is the most popular python framework ,it only work with python installed system,before starting Django we should install python.So lets see how to install python in windows 10. Fortunately installing python is so easy

Step 1)

Install python in windows 10

First you need to get the python installer,as of now latest python version that support Django is Python 3.6.7. Go to https://www.python.org/downloads/ and Download Python 3.6.7 and install don’t forget to tick add python 3.6 tp PATH,it will assign PATH variable

python-Django-step1

Step 2)

Check python installed successfully

open CMD in windows

c:/users>python
will respond like below
Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 13:35:33) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

python-Django-step2

Step 3)

install pip if not installed in your system

D:\my_project >

Step 4)

Create a project directory, you can create project directory anywhere in your system,here we are created in D drive with name my_project
then move current directory to project directory in CMD

D:\my_project >


Step 5)

Install virtual environment using pip
D:\my_project>pip install virtualenv

Create a virtual environment with name env_mysite you can rename anything as per your reference

D:\my_project>virtualenv env_mysite

install django in windows 10

Step 6)

Activate virtualenv
D:\my_project>env_mysite\scripts\activate

env_mysitePS D:\my_project>

Step 7)

Install django

env_mysitePS D:\my_project>pip install "django>=2.1,<2.2"

Step 8)

Create A New project

env_mysitePS D:\my_project>django-admin startproject myweb_site

Step 9)

Change Directory

env_mysitePS D:\my_project>Cd myweb_site

Step 10)

Run developemnt server

env_mysitePS D:\my_project> python manage.py runserver 

install-django-in-windows-10-step9

Finshed

Open http://127.0.0.1:8000/ in any browsers

install-django-in-windows-10-step-1

Congratulation you have installed Python django framework
Note:
Update latest changes in djnago

python manage.py makemigrations

python manage.py migrate

How to create a small project using Django

how to create new database user in mysql python

Step1

Set Mysql variable path

set path=%PATH%;C:\xampp\mysql\bin;

Step 2
Using command prompt login to databse

mysql -u root -p

Password NULL
create a database python

Step 3
Create new username and password
jack username
jackpassword is the password

c:\python>mysql -u root -p
Enter password: *
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.1.34-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'jack'@'localhost' IDENTIFIED BY 'jackpassword';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

Step 4
create new database using Python mysql connection

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="jack",
  passwd="jackpassword"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE myschool")

For Loop Syntax in Python

For Loop in Python

The for loop that is used is python,whenever we need repetition or iterate over a sequence .For Loop is one the frequently using loop in python.For loop has simple syntax .Let see the for loop syntax in details.

Here we are going to iterate over a sequence


#For Loop Syntax in Python

cars = ["SWIFT", "BELANO", "BMW"]
for x in cars:
  print(x) 

Output

SWIFT
BELANO
BMW

What are Python modules and packages, and how are they different?

Python modules and packages are ways to organize and reuse code, but they serve different purposes. Here’s a detailed breakdown of each and how they differ:


What is a Python Module?

  • A module is a single file of Python code that contains definitions and statements such as functions, classes, and variables.
  • The file must have a .py extension.
  • Modules make it easier to organize and reuse code by importing it into other scripts.

Example of a Module:

File: math_utils.py

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

You can use this module in another script:

import math_utils

result = math_utils.add(3, 5)
print(result)  # Output: 8

What is a Python Package?

  • A package is a directory that contains multiple modules and a special __init__.py file.
  • The __init__.py file (can be empty) marks the directory as a package and allows it to be imported like a module.
  • Packages help organize related modules into a single namespace.

Structure of a Package:

math_package/
    __init__.py
    addition.py
    subtraction.py

addition.py:

def add(a, b):
    return a + b

subtraction.py:

def subtract(a, b):
    return a - b

Use the package:

from math_package.addition import add
from math_package.subtraction import subtract

print(add(3, 5))        # Output: 8
print(subtract(10, 4))  # Output: 6

Key Differences Between Modules and Packages

FeatureModulePackage
DefinitionA single Python file (.py).A directory containing multiple modules.
PurposeOrganize code into reusable files.Group related modules into a namespace.
File StructureA single .py file.A directory with an __init__.py file.
ImportImport the .py file directly.Import modules from the package.
Exampleimport my_modulefrom my_package import my_module

How They Work Together

You can combine packages and modules to structure large projects effectively. For example:

my_project/
    __init__.py
    data_processing/
        __init__.py
        clean.py
        analyze.py
    visualization/
        __init__.py
        plot.py
        export.py

Usage:

from my_project.data_processing.clean import clean_data
from my_project.visualization.plot import create_plot

Summary:

  • A module is a single .py file containing Python code.
  • A package is a collection of modules grouped into a directory with an __init__.py file.
  • Modules are ideal for smaller, independent functionalities, while packages are better for organizing related functionality across multiple files.