【发布时间】:2020-10-05 05:24:10
【问题描述】:
我有一个大型模板文件here,我正在 for 循环中访问它以打印一个包含一些值的表。当用户单击其中一个值时,我希望它仅将 1 个 json 块转移到另一个页面,以使其预填充某些字段。我无法让我的程序在 json 字典中选择该单个条目以呈现下一页。以下是涉及的代码:
views.py
@apps.route('/add')
@login_required
@admin_required
def view_apps():
""" View available apps """
template = Template.query.all()
dir_path = os.path.dirname(os.path.realpath(__file__))
json_content = combine_json_templates()
print(json_content)
print(len(json_content))
return render_template('apps/add_app.html', apps=json_content)
def combine_json_templates():
master_list = []
cwd = os.getcwd()
print(cwd)
json_storage = 'app/storage/templates/json/'
for file in os.listdir(json_storage):
with open(json_storage + file) as json_path:
json_content = json.load(json_path)
for item in json_content:
master_list.append(item)
return master_list
@apps.route('/add/<int:app_id>')
@apps.route('/add/<int:app_id>/info')
def app_info(app_id):
app = json_content[""+app_id+""]
print(app)
add_app.html(显示表格并包含 onclick 的代码:
<div style="overflow-x: scroll;">
<table class="ui searchable sortable unstackable selectable celled table">
<thead>
<tr onclick="window.location.href = '{{ url_for('apps.app_info', app_id=apps.pop(['name'])) }}';">
<th class="sorted ascending">Title</th>
<th>Desctiption</th>
<th>Catagory</th>
<th>Platform</th>
</tr>
</thead>
<tbody>
{% for a in apps | sort(attribute='title') %}
<tr>
<td>{{ a.title }}</td>
<td>{{ a.description }}</td>
<td>{{ a.categories }}</td>
<td>{{ a.platform }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
我遇到问题的部分是获取一个唯一值,以便将其传递到下一页并引用正确的 json。
<tr onclick="window.location.href = '{{ url_for('apps.app_info', app_id=apps.pop(['name'])) }}';">
我应该使用不同的方法来引用 json 文件吗?我应该在加载 json 文件时修改它以添加某种密钥吗?
这是我的第一个烧瓶项目,所以我不确定最好的方法。
【问题讨论】:
-
“应用程序”中的索引是什么意思?
-
这绝对可以理解。我对烧瓶还是陌生的,所以有点混乱。有没有更好的方式让我与您取得联系并进行更深入的解释?
-
基本上在 onclick 函数应用程序中是一个包含目录的列表(每个目录都有上面链接的模板中的一个 json 块的内容)我不相信它现在还在排序。该视图是一个表格,其中显示了来自每个 json 块的信息(使用 for 循环),单击时我试图传递某种标识符以在下一页中引用所选字典(json 块及其内容)。
-
有没有办法让我们把它移到聊天中?
-
我不知道聊天,但想想我的答案。这就是你想要的吗?
标签: javascript python json flask