Web2py - how to input html
I have used rows.xml () to generate html output. I want to know how to add html codes to this generated html page, for example: "add logo, link css file, etc."
rows=db(db.member.membership_id==request.args[0]).select(db.member.membership_id
,db.member.first_name,db.member.middle_name
,db.member.last_name)
return rows.xml()
source to share
There are many HTML helpers you can use, for example:
html_code = A('<click>', rows.xml(), _href='http://mylink')
html_code = B('Results:', rows.xml(), _class='results', _id=1)
html_page = HTML(BODY(B('Results:', rows.xml(), _class='results', _id=1)))
etc.
You can even create an entire table automatically:
table = SQLTABLE(rows, orderby=True, _width="100%")
and then select it to insert links or modify its items.
It is very powerful and generally you don't need to write your own HTML. Here is a cheat sheet , or you can check the site documentation directly .
Edit: just to make sure you don't actually have to create the entire HTML page, it's easier to let web2py insert your response into a template with the same name as your controller (or force a specific template with response.view = 'template.html'
. The documentation tutorial will explain this better and in more detail.
In a few words, if you implement a function index
, you can either return a string (all the HTML of the page you want to navigate to) or a dictionary to use templates.
In the first case, just copy your function like this:
def index():
# ... code to extract the rows
return HTML(BODY(B('Results:', rows.xml(), _class='results', _id=1))).xml()
Otherwise, write an html template in views / controller / index.html (or another file if you inject response.view=...
into your function to reuse the same template) that could be:
<html><head></head>
<body>
{{=message}}
</body>
</html>
and return the dictionary:
def index():
# ... code to extract the rows
html = B('Results:', rows.xml(), _class='results', _id=1)
return dict(message=html)
source to share