2017-02-13 63 views
0

我試着用變形金字塔。但是形式不呈現爲形式,而是一個純粹的字符串變形金字塔渲染不正常。呈現爲純字符串

@view_config(route_name='sign_up', renderer='templates/sign_up.jinja2') 
def sign_up(request): 
    schema = SignUpForm().bind(request=request) 

    button = deform.form.Button(name='SignUp', title = 'Sign Up') 
    form = deform.form.Form(schema, buttons=(button,)) 

    if request.method == 'POST': 
     try: 
      appstruct = form.validate(request.POST.items()) 

      # Save the data to database 
      print('saved') 
      print(appstruct['username']) 

      request.session.flash('your have succesfully registered') 

      return HTTPFound('/') 
     except deform.exception.ValidationFailure as e: 
      rendered_form = form.render() 
    else: 
     print('rendering the form') 
     rendered_form = form.render(); 

    return {'rendered_form': rendered_form} 

這是使用Jinja2的模板我的HTML。在你的模板

<!DOCTYPE html> 
<html> 
    <head> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
    </head> 
    <body> 
      {{rendered_form}} 
    </body> 
</html> 

所有形式的信息顯示了在瀏覽器

class SignUpForm(deform.schema.CSRFSchema): 
    username = colander.SchemaNode(
     colander.String(), 
     title = 'Username') 
    password = colander.SchemaNode(
     colander.String(), 
     title = 'Password') 

回答

3

的Jinja2被配置爲純文本字符串像<form method=POST>等自動流出的任何變量,以避免從不受信任的標記跨站腳本攻擊變量。您可以通過{{ rendered_form | safe }}關閉表單的自動轉義(因爲可以信任變形以自行轉義呈現的數據)。

+0

完美解決方案!謝謝! – Bobby