Displaying data on new page in Python
<p>I want to display each row from a database in a <code>&lt;td&gt;</code> in the following index.html page:</p>

<pre><code>$def with(items)
&lt;h1&gt;&lt;/h1&gt;
&lt;br/&gt;
&lt;br&gt;
&lt;table border="1"&gt;
    &lt;tr&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;address&lt;/th&gt;
        &lt;th&gt;number&lt;/th&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
        &lt;td&gt;//want name here&lt;/td&gt;
        &lt;td&gt;//address here&lt;/td&gt;
        &lt;td&gt;//number here&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
</code></pre>

<p>app.py code:</p>

<pre><code>import web
import MySQLdb

urls = (
  '/', 'Index'
)

app = web.application(urls, globals())

render = web.template.render('templates/')

class Index(object):
    def GET(self):
        return render.hello_form()

    def POST(self):
            form = web.input(name="a", newname="s", number="d")
            conn = MySQLdb.connect(host= "localhost", user="root", passwd="", db="testdb")
            x = conn.cursor()
            x.execute("SELECT * FROM details  WHERE name = '%s'" % (form.name))
            conn.commit()
            items = x.fetchall()
            for row in items: 
                conn.rollback()
                conn.close()
                return render.index(items)
    if __name__ == "__main__":
        app.run()
</code></pre>

<p>items (results fetched) will be an array of tuples. Return the array as it is as you want to display whole data on the html and HTML is only rendered once.</p>

<p>app.py code</p>

<pre><code>import web
import MySQLdb

urls = (
  '/', 'Index'
)

app = web.application(urls, globals())

render = web.template.render('templates/')

class Index(object):
    def GET(self):
        return render.hello_form()

def POST(self):
        form = web.input(name="a", newname="s", number="d")
        conn = MySQLdb.connect(host= "localhost", user="root", passwd="", db="testdb")
        x = conn.cursor()
        x.execute("SELECT * FROM details  WHERE name = '%s'" % (form.name))
        items = x.fetchall()       
        conn.close()
        return render.index(items) // return the array of results as a whole
if __name__ == "__main__":
    app.run()
</code></pre>

<p>On index.html</p>

<pre><code>$def with(items)
&lt;h1&gt;&lt;/h1&gt;
&lt;br/&gt;
&lt;br&gt;
&lt;table border="1"&gt;
    &lt;tr&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;address&lt;/th&gt;
        &lt;th&gt;number&lt;/th&gt;
    &lt;/tr&gt;
   $for row in items: 
        &lt;tr&gt;
        &lt;td&gt;$row[0]&lt;/td&gt;
        &lt;td&gt;$row[1]&lt;/td&gt;
        &lt;td&gt;$row[2]&lt;/td&gt;
        &lt;/tr&gt;
&lt;/table&gt;

Iterate over the items and display them
</code></pre>

python python-2.7 web.py