Blog Details
Blog Title: | Use Oracle Database With Django In Windows | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Blogger: | hadoleamit@gmail.com | |||||||||
Image: | View | |||||||||
Content: |
In order to connect to Oracle from Python, we need to install cx_Oracle package, which is DB-API 2.0 implementation, using PIP. Getting Oracle Database ready We work with Oracle Database 11g Express Edition (XE). If you have not yet installed it, go to Oracle Database 11g Express Edition and download the version relevant to your platform.
pip install cx_Oracle
Installing Instant ClientIn order to access Oracle from Python, you need to install Instant Client that is specific to your platform. Go to Instant Client and select download for your platform. You need to download Instant Client 64 bit if your Python is 64 bit otherwise download 32 bit version. Python edition and Instant Client edition must be same. It is enough to download Instant Client Basic Light version as we need support for only English. You can check which version of Python you are using
Make sure Oracle's BIN directory or InstantClient folder is in system PATH.The following command shows how to add Oracle's BIN directory to system PATH: c:\>set PATH=%PATH%;C:\oraclexe\app\oracle\product\11.2.0\server\bin
If you are using InstantClient, use folder into which you installed InstantClient (for ex: c:\python\instantclient) as follows: c:\>set PATH=%PATH%;C:\python\instantclient
The following program will connect to Oracle Database using username 'gktcs' and password '12345'. In case you are trying to use a different account or a different version of Oracle database then feel free to change the details and experiment..
Creating Django Project and ApplicationWe need to install Django Framework using PIP as follows: pip install django Installing Django, installs django-admin.exe in Scripts folder of Python installation directory.
Let's create a new Django Project called oracledemo and Application called blog in that project with the following commands: django-admin startproject oracledemo It will create a new folder oracledemo and places manage.py and another folder oracledemo inside that. Get into oracledemo folder and then run manage.py to create a new application called blog inside that project. python manage.py startapp blog
Configure Django settings.py
Modify default database configuration so that Oracle is used as default database.
Creating ModelCreate a class that represents JOBS table in Oracle database in oracledemo/hr/models.py file.
Creating view and templateCreate the following function view in oracledemo/blog/views.py to display details of posts from Post table.
Here is oracledemo/blog/templates/blog_post.html to display details of Jobs in HTML table. blog_post.html
Finally add a new URL in oracledemo/oracledemo/urls.py that invokes blog_posts().
Start server after adding Oracle's bin directory or InstantClient directory to system PATH.
Now go to browser and enter the following URL to get list of Jobs. http://localhost:8000/post
|