Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TurboGears2-如何自定义新用户表单的布局?_Python_Forms_Web Frameworks_Turbogears2_Toscawidgets - Fatal编程技术网

Python TurboGears2-如何自定义新用户表单的布局?

Python TurboGears2-如何自定义新用户表单的布局?,python,forms,web-frameworks,turbogears2,toscawidgets,Python,Forms,Web Frameworks,Turbogears2,Toscawidgets,我目前正在将此用于我的新用户表单: class UserForm(AdminPage): entity = Model.User title = 'User' class child(ListForm): css_class = 'form-horizontal' buttons = [SaveButton(),CancelButton()] ... phone = TextField(

我目前正在将此用于我的新用户表单:

class UserForm(AdminPage):
    entity = Model.User
    title = 'User'
    class child(ListForm):
        css_class = 'form-horizontal'
        buttons = [SaveButton(),CancelButton()]
        ...
        phone = TextField(
                label = 'Phone',
                validator = twc.Required
        )
        ...
我假设我将不得不使用列表表单以外的其他东西来做我想做的事情。以下是我需要的:

我想自定义某些表单字段的长度,将两个表单字段相邻放置,而不是放在下面,并将这两个字段上的标签更改为显示在字段上方而不是左侧

我花了几个小时浏览了不同版本的tg文档和1.0API,但我找不到任何有用的东西。我试着加上:

__field_attrs__={'phone':{'rows':'2'}}
但一切都没有改变。我假设列表表单没有字段属性!?有没有人能为我指明正确的方向,告诉我如何实现我所追求的目标


非常感谢

您可以向字段添加CSS类和样式,如下所示:

phone = TextField(label='Phone',
                  validator=twc.Required,
                  css_class='MyTextFieldClass',
                  attrs=dict(style='display:block;width:8em', maxlength='12'))
对于完全不同的布局,您需要子类化
BaseLayout
,并引用您自己的模板,如下所述:

例如,我创建了一个更灵活的布局类FloatLayout:

from itertools import groupby
from tw2.core import Param
from tw2.forms.widgets import BaseLayout

class FloatLayout(BaseLayout):

    template = "widgets.templates.float_layout"

    aside = Param('All fields aside', default=False)

    def rows(self, children):
        """Create the rows."""
        def row_no(child, no=[0]):
            if not self.aside and not getattr(child, 'aside', False):
                no[0] += 1
            return no[0]
        return groupby(children, row_no)
它可以与此FloatForm类一起使用:

from tw2.core import Variable
from tw2.forms import Form

class FloatForm(Form):
    """Form using floating divisions allowing multiple fields per row.

    Fields having the 'aside' attribute set appear on the same row.

    Something like the following should be included in the site CSS file:

    form.floatform {
        margin-bottom: 1ex;
    }
    form.floatform div.row {
        clear: left;
        overflow: hidden;
        height: 100%;
        margin-top: 1.5ex;
    }
    form.floatform div.field {
        float: left;
        margin-right: 1em;
    }
    form.floatform label.fieldlabel {
        display: block;
    }
    form.floatform div.submit {
        margin-top: 3ex;
    }

    """

    template = "widgets.templates.float_form"

    child = Variable(default=FloatLayout)

    css_class = "floatform"
FloatLayout的Genshi模板float_layout.html如下所示:

<div xmlns:py="http://genshi.edgewall.org/" py:attrs="w.attrs" py:strip="True">
    <div py:for="row_no, row in w.rows(w.children_non_hidden)"
            class="${row_no % 2 and 'odd' or 'even'} row">
        <div py:for="child in row" py:attrs="child.container_attrs"
            class="field${child.validator and
                getattr(child.validator, 'required', None) and ' required' or ''}"
            title="${w.hover_help and w.help_text or ''}">
            <label py:if="child.label" for="${child.attrs.get('id')}"
                class="fieldlabel" py:content="child.label"/>
            <span py:replace="child.display()"/>
            <span py:if="not w.hover_help and child.help_text"
                class="fieldhelp" py:content="child.help_text"/>
            <span py:if="child.error_msg"
                class="fielderror" py:content="child.error_msg"/>
        </div>
    </div>
    <div py:if="w.children_hidden" style="display:none">
        <div py:for="child in w.children_hidden" py:replace="child.display()"/>
    </div>
</div>
<form xmlns:py="http://genshi.edgewall.org/"
    class="floatform" py:attrs="w.attrs">
    <div py:if="w.error_msg" class="formerror" py:content="w.error_msg"/>
    <div py:if="w.help_msg" class="formhelp"><p py:content="w.help_msg"/></div>
    <div py:replace="w.child.display()"/>
    <div py:for="button in w.buttons" class="field" py:content="button.display()"/>
</form>

如您所见,
remote\u account
new\u password
字段旁边有一个属性
使它们与
user\u name

显示在同一行。这不是关于TurboGears2的问题,而是关于ToscaWidgets2的问题。1.0 API是什么意思?我找不到2.2或2.0 API,这就是我研究1.0版本的原因。是的,我想它是关于ToscaWidgets的,我对这一切还是新手。谢谢,我们将进一步查看。请注意,TurboGears 1(支持内部小部件和由其开发的ToscaWidgets 1)和TurboGears 2(支持ToscaWidgets 1和ToscaWidgets 2,这也是进一步的开发)。所有这些都有重要的区别,所以始终使用适当的文档(最新的TurboGears 2文档:,最新的ToscaWidgets 2文档:)。我尝试如上所述添加css,但它完全没有作用。什么都没有是什么意思?你没有在HTML中得到输入字段的样式和css属性吗?哈!你对代码所做的更改现在成功了。加上“他们”使它这样的宽度实际上改变了这一次。非常感谢。我将进一步查看CAWIDGETS2,以了解如何完成我需要的其他两件事(更改标签位置并将两个字段对齐)。谢谢。通过设置
display:block
的样式,文本字段也应该显示在标签下方自己的一行中。现在我添加了一个示例
FloatForm
,它为您提供了充分的灵活性。
class UserForm(FloatForm):
    action = url('save_user')
    submit = SubmitButton('Save user')
    user_id = HiddenField(validator=IntValidator())
    user_name = TextField(validator=UserNameValidator(max=16),
        size=20, maxlength=16, label=u'User name:')
    remote_account = CheckBox(validator=BoolValidator(),
        label=u'Remote account:', aside=True)
    new_password = PasswordField(
        validator=PasswordValidator(required=False),
        size=20, maxlength=16, label=u'Password:', aside=True)
    group_id = CheckBoxList(item_validator=IntValidator(),
        label=u'Roles:', css_class='inline')
    display_name = TextField(validator=NameValidator(max=255),
        size=64, maxlength=255, label=u'Real name:')
    mail = TextField(validator=EmailIfLocalValidator(),
        size=64, maxlength=255, label=u'Email address:')