Search code examples
pythonjinja2pyramiddeformcolander

deform Sequence of Mapping not updating


I have created a view which output a form containing:

class Person(colander.Schema):
    name = colander.SchemaNode(colander.String())
    age = colander.SchemaNode(colander.Integer(),
                              validator=colander.Range(0, 200))

class People(colander.SequenceSchema):
    person = Person()

class Schema(colander.Schema):
    people = People()

class SchemaSpace(colander.MappingSchema):
    schema = Schema()
    corners = colander.SchemaNode(colander.String())

The output is correct but when I click the button Add Person, nothing happen. The page blink and scrolls up and nothing, I fill anything. Do you know what is happening here? I am using multiples forms, is it coming from here?

My HTML is small and I am using jinja2:

{% extends "layout.jinja2" %}
{% block head %}

<script src="{{request.static_url('deform:static/scripts/jquery-2.0.3.min.js')}}"
                type="text/javascript"></script>
<script src="{{request.static_url('deform:static/scripts/bootstrap.min.js')}}"
        type="text/javascript"></script>

<script src="{{request.static_url('deform:static/scripts/jquery.form-3.09.js')}}"
            type="text/javascript"></script>
<script src="{{request.static_url('deform:static/scripts/jquery-sortable.js')}}"
            type="text/javascript"></script>
<script src="{{request.static_url('deform:static/scripts/jquery.maskedinput-1.3.1.min.js')}}"
            type="text/javascript"></script>

{% endblock head %}

{% block content %}
<div class="row">
  <div class="col-md-8">
    <div class="content">
    <h1>{{view.view_name}} - {{page_title}}</h1>
    <p>Welcome</p>

    {{form | safe}}

    </div>
  </div>
  <div class="col-md-4">
    <h2>Extra</h2>
    <p>some text</p>

  </div>
</div>
{% endblock content %}

From what I found from the doc, I added some scripts. But I had some issues so I manually entered them. Using:

<tal:block tal:repeat="reqt view.reqts['css']">
  <link rel="stylesheet" type="text/css"
    href="${request.static_url(reqt)}"/>
</tal:block>

Issue was:

<script type="text/javascript" src="{{request.static_url(reqt)}}"></script>
File "/Users/roy/Applications/miniconda3/envs/WebAppBatman/lib/python3.6/site-packages/pyramid/url.py", line 644, in static_url
  if not os.path.isabs(path):
File "/Users/roy/Applications/miniconda3/envs/WebAppBatman/lib/python3.6/posixpath.py", line 64, in isabs
  s = os.fspath(s)
TypeError: expected str, bytes or os.PathLike object, not Undefined

To be complete, here is the view:

@view_config(renderer='templates/settings.jinja2')
def settings(self):
    html = []
    if 'submit' in self.request.POST:
        posted_formid = self.request.POST['__formid__']
        for (formid, form) in forms.items():
            if formid == posted_formid:
                try:
                    controls = self.request.POST.items()
                    form['captured'] = form['form'].validate(controls)


                    html.append(form['form'].render(form['captured']))
                except deform.ValidationFailure as e:
                    # the submitted values could not be validated
                    html.append(e.render())
            else:
                if form['captured'] is not None:
                    html.append(form['form'].render(form['captured']))
                else:
                    html.append(form['form'].render())
    else:
        for _, form in forms.items():
            html.append(form['form'].render())

    reqts = forms['form1']['form'].get_widget_resources()

    html = ''.join(html)

    # values passed to template for rendering
    return {'form': html, 'page_title': 'Settings', 'reqts': reqts}

Solution

  • I got it working! It was due to the scripts as I suspected. Here is a working syntax using jinja2:

    {% for reqt in reqts['js'] %}
    <script src="{{request.static_url(reqt)}}"
                type="text/javascript"></script>
    {% endfor %}
    

    I was not able to make it work using tap:repeat.