Madawar
4/27/2014 - 5:33 PM

Using Html Fill in Pyramid Framework

Using Html Fill in Pyramid Framework

from pyramid.view import view_config
from pyramid.renderers import render
from formencode import htmlfill
@view_config(route_name='home', renderer='form_view.mak')
def my_view(request):
    form = render('templates/form_partial.mak',
                    {},
                    request=request)
    defaults=dict(name="Dennis Wanyoike", age="18")
    form = htmlfill.render(form, defaults)
    return dict(form=form)
<!DOCTYPE html>
<html lang="${request.locale_name}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="pyramid web application">
        <meta name="author" content="Pylons Project">
        <link rel="shortcut icon" href="${request.static_url('formencodetut:static/pyramid-16x16.png')}">

        <title>Starter Template for The Pyramid Web Framework</title>

        <!-- Bootstrap core CSS -->
        <link href="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">

        <!-- Custom styles for this template -->
        <link href="${request.static_url('formencodetut:static/assets/css/bootstrap-theme.min.css')}" rel="stylesheet">
        <link href="${request.static_url('formencodetut:static/assets/css/bootstrap.css')}" rel="stylesheet">
        <link href="${request.static_url('formencodetut:static/assets/css/font-awesome.min.css')}" rel="stylesheet">

        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
          <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
          <script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
        <![endif]-->
        <style>
            body {
                background: #ccc;
                font: 14px sans-serif;

            }
            .navbar{
                border-radius: 0px;
            }
            .letter {
                background: #fff;
                box-shadow: 0 0 10px rgba(0,0,0,0.3);
                margin: 26px auto 0;
                padding: 24px;
                position: relative;
                width: 98%;
            }
            .letter:before, .letter:after {
                content: "";
                height: 98%;
                position: absolute;
                width: 100%;
                z-index: -1;
            }
            .letter:before {
                background: #fafafa;
                box-shadow: 0 0 8px rgba(0,0,0,0.2);
                left: -5px;
                top: 4px;
                transform: rotate(-2.5deg);
            }
            .letter:after {
                background: #f6f6f6;
                box-shadow: 0 0 3px rgba(0,0,0,0.2);
                right: -3px;
                top: 1px;
                transform: rotate(1.4deg);
            }
        </style>
    </head>

    <body>
        <div class="container">
            <div class="body-content letter">
                <nav class="navbar navbar-default" role="navigation">
                    <div class="container-fluid"><!-- This keeps the margins nice -->
                        <div class="navbar-header">
                            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                                <span class="sr-only">Toggle navigation</span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                            </button>
                            <a class="navbar-brand" href="www.codedcell.com">Coded Cell </a>
                        </div><!-- /.navbar-header -->
                        <div class="collapse navbar-collapse navbar-ex1-collapse">
                            <ul class="nav navbar-nav">

                            </ul>


                            <ul class="nav navbar-nav navbar-right">

                            </ul>
                        </div><!-- /.navbar-collapse -->
                    </div><!-- /.container-fluid -->
                </nav>
                <!--Body content-->
                <div class="row">
                    <div class="col-md-12" id="app-dock">
                        <h1 class="text-center"><small>Form Encode Tutorial</small></h1>
                        <hr/>
                       ${form|n}


                    </div>
                </div>
                <!--End of body content-->

                <footer>

                </footer>
            </div>
        </div>



        <!-- Bootstrap core JavaScript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->

    </body>
</html>
<form method="POST" action="">
    <div class="form-group ">
        <label for="name">Your Name</label>
        <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-user"></i></span>
            <input type="text" class="form-control" name="name" id="name" placeholder="John Doe">
        </div>
        <p class="help-block">Please enter your full names</p>
    </div>
    <div class="form-group ">
        <label for="age">Your Age</label>
        <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-user"></i></span>
            <input type="text" class="form-control" name="age" id="age" placeholder="18">
        </div>
        <p class="help-block">Please enter your age</p>
    </div>

    <div class="form-group ">
        <label for="gender">Please select your sex</label>
        <select name="gender" class="form-control">
            <option value="Male">Male</option>
            <option value="Female">Female</option>
        </select>     
    </div>
    <button type="submit" class="btn btn-block">Submit</button>
</form>