Jul 20, 2024

Django + XAMPP [Python - Web Framework, MySQL - Database]

Django + XAMPP [Python - Web Framework, MySQL - Database]


Requirements: XAMPP(or MariaDB) or MySQL | Python 3.x

Overview: To host a website using Django as Python web Framwork and MySQL as DataBase server






Step 1: Install the required softwares python, SQL server
Step 2: Open your terminal or command prompt, Install MySQL connector module using PIP

    
pip install mysql-connector-python
    
Copy
Copied
    
pip install mysql-connector-python
    


Step 3: create a new Django project by copy pasting the below code followed by hitting enter button.

    
django-admin startproject sample
    
Copy
Copied
    
django-admin startproject sample
    




Step 4: create a new Django App by copy pasting the below code followed by hitting enter button.

    
cd projectname
django-admin startapp demo
    
Copy
Copied
    
cd projectname
django-admin startapp demo
    

or

    
python manage.py startapp demo
    
Copy
Copied
    
python manage.py startapp demo
    




Step 5: create a new python file named urls.py inside app 'demo' and copy pasting the below code .

    
from django.urls import path
from . import views

urlpatterns = [
    path('demo/', views.demo, name='demo'),
]
    
Copy
Copied
    
from django.urls import path
from . import views

urlpatterns = [
    path('demo/', views.demo, name='demo'),
]
    







Step 6: Modify the python file named urls.py outside app (in project folder 'sample') and copy pasting the below code .

    
from django.contrib import admin
from django.urls import path
from django.urls import include

urlpatterns = [
    path('', include('demo.urls')),
    path('admin/', admin.site.urls),
]
    
Copy
Copied
    
from django.contrib import admin
from django.urls import path
from django.urls import include

urlpatterns = [
    path('', include('demo.urls')),
    path('admin/', admin.site.urls),
]
    






Step 7: Goto sample project and inside demo app directory modify the content inside views.py (create a function demo)

    
# from django.shortcuts import render
from django.http import HttpResponse
import mysql.connector as connector

# Create your views here.
def demo(request):
    mydb = connector.connect(
    host = "localhost",
    user = "root",
    password = "root",
    port = "3306",
    database = "mysql"
)
    cursor = mydb.cursor()
    # sql='''CREATE TABLE employees(emp_id int, emp_name varchar(255), salary int);'''
    # cursor.execute(sql)
    sql='''INSERT INTO employees (emp_id, emp_name, salary) VALUES(1, 'John', 10000);'''
    cursor.execute(sql)
    sql='''INSERT INTO employees (emp_id, emp_name, salary) VALUES(2, 'Jaya', 10000);'''
    cursor.execute(sql)
    sql='''INSERT INTO employees (emp_id, emp_name, salary) VALUES(3, 'Rabe', 10000);'''
    cursor.execute(sql)
    sql='''INSERT INTO employees (emp_id, emp_name, salary) VALUES(4, 'Manuel', 10000);'''
    cursor.execute(sql)
    sql='''SELECT * FROM employees;'''
    cursor.execute(sql)
    result = cursor.fetchall()
    return HttpResponse( f'Hello World !{result}' )
    
Copy
Copied
    
# from django.shortcuts import render
from django.http import HttpResponse
import mysql.connector as connector

# Create your views here.
def demo(request):
    mydb = connector.connect(
    host = "localhost",
    user = "root",
    password = "root",
    port = "3306",
    database = "mysql"
)
    cursor = mydb.cursor()
    # sql='''CREATE TABLE employees(emp_id int, emp_name varchar(255), salary int);'''
    # cursor.execute(sql)
    sql='''INSERT INTO employees (emp_id, emp_name, salary) VALUES(1, 'John', 10000);'''
    cursor.execute(sql)
    sql='''INSERT INTO employees (emp_id, emp_name, salary) VALUES(2, 'Jaya', 10000);'''
    cursor.execute(sql)
    sql='''INSERT INTO employees (emp_id, emp_name, salary) VALUES(3, 'Rabe', 10000);'''
    cursor.execute(sql)
    sql='''INSERT INTO employees (emp_id, emp_name, salary) VALUES(4, 'Manuel', 10000);'''
    cursor.execute(sql)
    sql='''SELECT * FROM employees;'''
    cursor.execute(sql)
    result = cursor.fetchall()
    return HttpResponse( f'Hello World !{result}' )
    




Step 8: Finally run the Django server by copy pasting the below code in terminal and open http://127.0.0.1:8000/demo in browser

    
python manage.py runserver
    
Copy
Copied
    
python manage.py runserver
    




Full Video Tutorial: