使用金字塔从表中选择一行
我想构建(构建表格很容易,我在模板内完成)一个表格并从中选择一行。我使用金字塔作为框架,我认为我需要以某种方式使模板与模型对话,但我不确定如何做。有人可以向我展示一个示例或指导我链接到可以看到解释和示例的链接(我找不到)。这是我的表格 HTML:
<table border="1">
<tr>
<th>Course Number</th>
<th>Course Name</th>
</tr>
<tr>
<td>111</td>
<td>What ever the name is</td>
</tr>
</table>
I would like to construct (constructing a table is easy, I do it inside templates) a table and choose a row from it. I am using pyramid as a framework and I think somehow I need to make the templates talk to the model, but I am not sure how. Can someone show me an example or direct me to link where I can see an explanation and an example (I couldn't find one). This is my HTML for the table:
<table border="1">
<tr>
<th>Course Number</th>
<th>Course Name</th>
</tr>
<tr>
<td>111</td>
<td>What ever the name is</td>
</tr>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
做出一些假设,您所描述的内容将通过 ajax 处理,这是一种基于 javascript 的技术,并且超出了 Pyramid 框架的范围,但很容易添加。下面是一个非常简单的 HTML 示例。
标签之间的代码是 javascript,这里我使用 jQuery 库创建对 url '/some_pyramid_url_to_a_view/' 的 ajax 调用,该调用与视图函数 'some_pyramid_view' 绑定。我使用“row”类“$('.row').bind('click',...”上的 jQuery 选择器将 click 事件绑定到表中的行元素,然后该事件由紧随其后的功能块“function(){...}”在选项对象中设置调用,并在数据“data:{'row_id':$(this).attr('”中传递行 ID。 id')}," 其中代码"$(this).attr('id')" 正在访问模板 '...' 中设置的 id 最后,我使用 '$.ajax(options)' 将请求发送到视图
。 ,JSON 用于从金字塔视图与 javascript 进行通信。JSON 代表 JavaScript 对象表示法,是一种数据交换格式,我创建了一个 Python 字典,并使用“json”包将其转换为 JSON,然后将其发送回。到javascript 中的“成功”回调函数包含方法调用的结果。在成功回调函数中,我使用视图中的方法调用结果更新表行中的文本。 event.data.method_result)'
这可能超出您的预期,但非常值得学习如何使用 ajax 向您的网站添加功能。
Making some assumptions, what you describe would be handled via ajax, which is a technology based on javascript, and outside the scope of they Pyramid framework but easily added. Below is a very simple example of what the HTML might look like.
The code between the tags is the javascript, here I am using the jQuery library to create the ajax call to the url '/some_pyramid_url_to_a_view/' which is tied to the view function 'some_pyramid_view'. I'm binding a click event to the row element in the table using the jQuery selector on the class 'row' "$('.row').bind('click',..." the event is then handled by the function block "function(){...}" immediately following. I'm setting up the call in the options object and passing the row id in the data "data:{'row_id':$(this).attr('id')}," where the code "$(this).attr('id')" is accessing the id set in the template '...' Finally I send of the request to the view using '$.ajax(options)'.
There is another piece here, JSON is being used to communicate back to the javascript from the Pyramid view. JSON stands for JavaScript Object Notation and is a data-interchange format. I create a Python dictionary and using the 'json' package convert it to JSON which I send back to the 'success' call back function in the javascript with the results of the method call. In the success call back function I update the text in the table row with the result of the method call in the view. '$(row).text(event.data.method_result)'
This might be more than you were expecting but it is well worth learning how to use ajax to add functionality to your websites.