How to integrate Postgresql db with Django,so that data will be retrieved from that postgresql db table and show it on browser via html

707 views
Skip to first unread message

Swastik Acharya

unread,
Dec 3, 2013, 1:42:10 AM12/3/13
to django...@googlegroups.com
django 1.4 version
postgresql 9.1 version

I have successfully loaded all the html files and css on browser using django.
Now  i have some tables in postgresql ,which i need to integrate it with django and .html files so that data will be read and execute both from html file inputs and database table.
what are the steps to achieve this ?

the home.html contains the macid,hostname,blacklisted etc whatever are there in postgresql table.
i just need to connect and file up those blank spaces in html with data from postgresql table.

for ex:
my folder structure is
>folder
>mysite
>app
            -static
                -style.css
                -main.css
            -templates
             -home.html
            -init.py and init.pyc
            -models.py and models.pyc
            -test.py
            -views.py

        >mysite
         -init.py
         -settings.py and settings.pyc
         -urls.py and urls.pyc
         -views.py and views.pyc
         -wsgi.py
>mysite.db
>manage.py    


and in postgresql the table name is device_table
and its contents are:


CREATE TABLE device_table
(
  serial_no serial NOT NULL,
  macid character varying(100) NOT NULL,
  date_time_of_installation timestamp without time zone,
  blacklisted boolean,
  activated integer,
  deactivated boolean,
  reason character varying(500),
  hostname character varying(255),
  host_ip character varying(50),
  agent_version character varying(75),
  CONSTRAINT device_table_pkey PRIMARY KEY (macid)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE device_table OWNER TO postgres;

Note:I have created models.py by running inspectdb command.

Waiting for a solution..apart from solutions any guide/advice/suggestion are mostly welcome


Tom Lockhart

unread,
Dec 3, 2013, 8:28:50 AM12/3/13
to django...@googlegroups.com

On 2013-12-02, at 10:42 PM, Swastik Acharya <swas...@gmail.com> wrote:

> django 1.4 version
> postgresql 9.1 version
>
> I have successfully loaded all the html files and css on browser using django.
> Now i have some tables in postgresql ,which i need to integrate it with django and .html files so that data will be read and execute both from html file inputs and database table.
> what are the steps to achieve this ?
>

If you have a working Django installation, you just need your models.py to have a definition matching your existing table(s) and you will probably want to have an admin.py to give you easy first access to these tables.

> the home.html contains the macid,hostname,blacklisted etc whatever are there in postgresql table.
> i just need to connect and file up those blank spaces in html with data from postgresql table.
>

Have you worked through the Django tutorial?

You will need your home.html to retrieve your table items as context variables. Having a views.py with a view inheriting from ListView will probably be helpful here. Then your urls.py at the top level of your site needs to map the home page pattern ("^$") to your view.
Does your models.py have a Meta class with a line

db_table="device_table"

?

Please post your urls.py and models.py and also give details on what specific problem you are seeing.

hth

- Tom

Message has been deleted
Message has been deleted

Swastik Acharya

unread,
Dec 4, 2013, 2:48:11 AM12/4/13
to django...@googlegroups.com
in the mean time i was able to populate datas's from postgres table by modifying the models.py and views.py as follows::

urls.py
from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'mysite.views.home', name='home'),
    url(r'^blisteddevies/$', 'mysite.views.blisteddevies', name='blisteddevies'),
    url(r'^admin/', include(admin.site.urls)),
)

Models.py:
from __future__ import unicode_literals

from django.db import models


class DeviceTable(models.Model):
    serial_no = models.IntegerField()
    macid = models.CharField(max_length=100, primary_key=True)
    class Meta:
        db_table = 'device_table'

VIEWS.py::
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.shortcuts import render
from app.models import DeviceTable



def home(request):
    rows = DeviceTable.objects.all()
    data = []
    for row in rows:
        data.append(row.macid)
        
    return render_to_response('app/home.html', {'data':data})

def blisteddevies(request):
    return render_to_response(
    'app/blisteddevies.html',
    )

and in app folder i hav created admin.py::

from django.contrib import admin
from app.models import DeviceTable

admin.site.register(DeviceTable)



NOW what i require is :if i select tick button against that macid to be blacklisted and click on save it should save it to postgres database table and also show it on the blacklisted page when i access blacklisted.html page via django.
Please guide me and i have also attached some screenshots for better idea of scenario.

Thank you

blacklist_page.png
home.png
Reply all
Reply to author
Forward
0 new messages