c:\>set PATH=%PATH%;C:\oraclexe\app\oracle\product\11.2.0\server\bin
c:\>set PATH=%PATH%;C:\python\instantclient
import os import cx_Oracle # Connect to hr account in Oracle Database 11g Express Edition(XE) con = cx_Oracle.connect("hr", "hr", "localhost/xe") print("Connected!") con.close()
pip install django
django-admin startproject oracledemo
python manage.py startapp hr
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'hr', ]
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.oracle', 'NAME': 'XE', 'USER': 'hr', 'PASSWORD': 'hr', 'HOST': 'localhost', 'PORT': '1521' } }
class Job(models.Model): job_id = models.CharField(max_length=10,primary_key=True) job_title = models.CharField(max_length=50, null = True) min_salary = models.IntegerField() max_salary = models.IntegerField() class Meta: db_table = "jobs"
def list_jobs (request): return render(request,'list_jobs.html',{'jobs' : Job.objects.all()})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Jobs</title> </head> <body> <h1>Jobs</h1> <table width="100%" border="1"> <tr style="background-color:lightgray"> <th>ID</th> <th>Title</th> <th>Min Salary</th> <th>Max Salary</th> </tr> {% for job in jobs %} <tr> <td>{{job.job_id}}</td> <td>{{job.job_title }}</td> <td>{{job.min_salary}}</td> <td>{{job.max_salary}}</td> </tr> {% endfor %} </table> </body> </html>
from django.urls import path import hr.views as hr_views urlpatterns = [ ... path('jobs', hr_views.list_jobs) ]
>set PATH=%PATH%;C:\oraclexe\app\oracle\product\11.2.0\server\bin >python manage.py runserver
http://localhost:8000/jobs