2

I have a jQuery table plugin (that is given data from a python script) that I want to add to my pyramid template, although when I run my instance and view the rendered webpage from the template my widget does not show up. I the only thing I can imagine to do is to use deform to make sure that the jQuery can get the correct resources that are linked in the header of the template. Except I am unsure of how I integrate deform to solve my problem.

Here is my init.py:

from pyramid.config import Configurator
from sqlalchemy import engine_from_config

from .globals import Session, Base

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    Session.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.reflect()

    config = Configurator(settings=settings)
    config.add_static_view('static', 'static/', cache_max_age=3600)
    #config.add_static_view('static', 'deform:static')
    config.add_route('Building', '/Building')
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()

Here is my template file 'mytemplate.pt':

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
    <link rel="stylesheet" href="../static/pylons.css" type="text/css" media="screen" charset="utf-8" />
    <link rel="stylesheet" href="../static/JQueryPlugins/Flexigrid/css/flexigrid.pack.css" type="text/css" media="screen" charset="utf-8" />

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="../static/JQueryPlugins/Flexigrid/js/flexigrid.pack.js"></script>

</head>
<body>

<div id="container">
<div id="logo"><img src="../static/banner.png" alt="Logo" height="117" width="619" /> </div>
<div id="leftcolumn">
  <ul class="navBar">
  </ul>
</div>
<div id="rightcolumn">
<h1>Physical Plant</h1>
<br />
<script type="text/javascript">
$(".table_id").flexigrid({
    url: '',
    dataType: '',
    colModel : [
        {display: 'ID', name : 'id', width : 40, sortable : true, align: 'center'},
        {display: 'Name', name : 'name', width : 180, sortable : true, align: 'left'},
        ],
    searchitems : [
        {display: 'ID', name : 'id'},
        {display: 'Name', name : 'name', isdefault: true}
        ],
    sortname: "id",
    sortorder: "asc",
    usepager: true,
    title: 'Buildings',
    useRp: true,
    rp: 15,
    showTableToggleBtn: true,
    width: 700,
    height: 200
});   
</script>
<table class="table_id">
<div id="datacolumn" tal:replace="structure data" />

</table>



<script type="text/javascript">
   deform.load()
</script>
</div>
</body>
</html>

Here is my view.py:

import csv
import deform
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
from .globals import Session, Base
from sqlalchemy.exc import DBAPIError

from .KeyDatabase import Building
session = Session()
config = Configurator()
"""
form = deform.Form(someschema)
resources = form.get_widget_resources()
js_resources = resources['js']
css_resources = resources['css']
js_links = [ '%s' % r for r in js_resources ]
css_links = [ 'http://my.static.place/%s' % r for r in css_resources ]
js_tags = ['<script type="text/javascript" src="%s"></script>' % link
           for link in js_links]
css_tags = ['<link rel="stylesheet" href="%s"/>' % link
           for link in css_links]
tags = js_tags + css_tags
return {'form':form.render(), 'tags':tags}
"""
def list_entity(entity_class):
    table = ""
    header = False

    for entity in session.query(entity_class):
        if not header:
            table += entity.table_header() + " \n "
            header = True
        table += entity.table_row() + " \n "

    table += ""
    return table

#@view_config(route_name='home', renderer="templates/mytemplate.pt")
#def my_view(request):
#return {'project':'MyProject'}

@view_config(route_name='home', renderer="templates/mytemplate.pt")
def list_Building(request):
    try:
        return{'data':list_entity(Building)}
    except DBAPIError:
        return Response(conn_err_msg, content_type='text/plain', status_int=500)
4

1 回答 1

2

我看不出 Deform 与您的问题有何关系 - 看起来您只需要修复模板中 JS 文件的路径即可。

<head>
    <link rel="stylesheet" href="/static/pylons.css" type="text/css" media="screen" charset="utf-8" />
    <link rel="stylesheet" href="/static/JQueryPlugins/Flexigrid/css/flexigrid.pack.css" type="text/css" media="screen" charset="utf-8" />

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="/static/JQueryPlugins/Flexigrid/js/flexigrid.pack.js"></script>

</head>

使用 Firebug 或其他工具检查所有资源是否正确加载

于 2012-04-27T07:02:54.073 回答