forms.py

来自「python web programming 部分」· Python 代码 · 共 36 行

PY
36
字号
class FormsMixin:

    def DropDown(self, tbl, valcol, descol, name="pulldown",
                default="Please make a selection",
                selected=None, ordercol=None, autosubmit=None):

        """Create a dropdown selector from two columns of a database table.

        Note that column aliasing is used to give fixed attribute names to
        the columns retrieved, so this algorithm works with two different
        database columns or the same one for both value and description."""

        td = dtuple.TupleDescriptor([("ValCol",), ("DesCol",)])
        if autosubmit:
            auto = """OnChange="submit(this.parent)"""
        else:
            auto = ""
        result = ["""<SELECT NAME="%s" %s ">""" % (name, auto)]
        result.append(self.Option("", default))
        stmt = "SELECT %s, %s FROM %s" % (valcol, descol, tbl)
        if ordercol:
            stmt += " ORDER BY %s" % ordercol
        # print stmt
        self.cursor.execute(stmt)
        rows = self.cursor.fetchall()
        for row in rows:
            row = dtuple.DatabaseTuple(td, row)
            result.append(self.Option(row.ValCol, row.DesCol))
        result.append("</SELECT>\n")
        return "\n".join(result)

    def Option(self, val, dsc):
        return """<OPTION VALUE="%s">%s""" % (val, dsc)


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?