我需要一个解决方案来定向到更深的url层(如果此语句正确)。 登录后,用户可以看到两个按钮,当前网址为
/tools
单击一个按钮后,它会转到一个新页面,其URL现在为
/tools/swi
其中 swi 是按钮的名称。此页面中还有另外两个按钮,其中一个是 view 。我想点击它并显示网址
/tools/swi/table
但是我不知道如何实现。因为如果我单击工具页面中的另一个按钮,而不是单击 view ,我需要
/tools/dsm/table
现有代码如下: 工具按钮的按钮字典
toolsdict = {
"swi":["swi"],
"dsm":["dsm"]
}
路线代码:
@app.route('/tools',methods=['POST','GET'])
@login_required
def tools():
return render_template('tools.html',title='Tools',Tools=toolsdict)
@app.route('/tools/<string:subt>',methods=['POST','GET'])
@login_required
def subtool(subt):
htmlname = subt+".html"
return render_template(htmlname,title=list(toolsdict.keys())[list(toolsdict.values()).index([subt])])
@app.route('/table', methods=['GET', 'POST'])
@login_required
def table_edit():
extraction = PDF.query.all()
labels = db.Model.metadata.tables['pdf_info'].c.keys()
return render_template('table.html',
title='Table edit', labels=labels, extraction=extraction)
我尝试更改为
@app.route('/tools/<string:subt>/table', methods=['GET', 'POST'])
但是我不知道如何将选定的按钮值传递给 subt 变量。
HTML代码:
tools.html
{% extends "select.html" %}
{% block selection %}
{% for k in Tools.keys() %}
<a href="{{ url_for('subtool',subt=Tools[k][0]) }}" class="btn btn-lg btn-primary btn-sm">{{ k }}</a>
{% endfor %}
{% endblock %}
swi.html
{% extends "select.html" %}
{% block selection %}
<a href="{{ url_for('table_edit') }}" class="btn btn-lg btn-primary btn-sm">View</a>
<a href="{{ url_for('add_entry') }}" class="btn btn-lg btn-primary btn-sm">Create</a>
{% endblock %}
table.html的一部分
<table class="table table-striped table-hover">
<thead>
<tr>
{% for label in labels %}
<td>{{ label }}</td>
{% endfor %}
<td></td>
<td></td>
</tr>
</thead>
{% for ex in extraction %}
<tr class={{ "success" if ex.done }}>
<td>{{ ex.id }}</td>
<td>{{ ex.author }}</td>
<td>{{ ex.title }}</td>
<td>{{ ex.version }}</td>
<td>{{ ex.last_update }}</td>
<td>{{ ex.filepath }}</td>
<td>{{ ex.uid }}</td>
<td><a href="edit/{{ ex.id }}" class="btn btn-secondary pull-left btn-sm">Edit</a></td>
<td>
<form action="{{ url_for('delete',id=ex.id) }}" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td>
<td><a href="{{ url_for('pdf_open',fn=ex.title) }}" class="btn btn-warning pull-left btn-sm">Open</a></td>
</tr>
{% endfor %}
</table>
有人可以帮忙吗?谢谢。
答案 0 :(得分:0)
尝试这些修改
@app.route('/tools', methods=['POST','GET'])
def tools():
headers = {'Content-Type': 'text/html'}
return make_response(render_template('tools.html',title='Tools',Tools=toolsdict), 200, headers)
@app.route('/tools/<string:subt>', methods=['POST','GET'])
def subtool(subt):
headers = {'Content-Type': 'text/html'}
return make_response(render_template('{}.html'.format(subt), title=list(toolsdict.keys())[list(toolsdict.values()).index([subt])])
, 200, headers)
@app.route('/tools/<string:buttonName>/table', methods=['GET', 'POST'])
def table_edit(buttonName):
return render_template('table.html', whoami=buttonName, title='Table edit')
@app.route('/tools/<string:buttonName>/addentry', methods=['GET', 'POST'])
def add_entry(buttonName):
return render_template('table.html', whoami=buttonName, title='Add entry')
base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test App</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" />
</head>
<body>
<section class="hero is-primary is-fullheight">
<div class="hero-head">
<nav class="navbar">
<div class="container">
<div id="navbarMenuHeroA" class="navbar-menu">
<div class="navbar-end">
<a href="{{ url_for('tools') }}" class="navbar-item">
Tools
</a>
</div>
</div>
</div>
</nav>
</div>
<div class="hero-body">
<div class="container has-text-centered">
{% block content %}
{% endblock %}
</div>
</div>
</section>
</body>
</html>
tools.html
{% extends "base.html" %}
{% block content %}
{% for k in Tools.keys() %}
<a href="{{ url_for('subtool', subt=Tools[k][0]) }}" class="btn btn-lg btn-primary btn-sm">{{ k }}</a>
{% endfor %}
{% endblock %}
swi.html
{% extends "base.html" %}
{% block content %}
<div>
<a href="{{ url_for('table_edit', buttonName='swi') }}" class="btn btn-lg btn-primary btn-sm">View</a>
<a href="{{ url_for('add_entry', buttonName='swi') }}" class="btn btn-lg btn-primary btn-sm">Create</a>
</div>
{% endblock %}
dsm.html
{% extends "base.html" %}
{% block content %}
<div>
<a href="{{ url_for('table_edit', buttonName='dsm') }}" class="btn btn-lg btn-primary btn-sm">View</a>
<a href="{{ url_for('add_entry', buttonName='dsm') }}" class="btn btn-lg btn-primary btn-sm">Create</a>
</div>
{% endblock %}
table.html
{% extends "base.html" %}
{% block content %}
<table class="table table-striped table-hover">
<thead>
<tr>
<td>Header for {{ whoami }}</td>
</tr>
</thead>
<tbody>
<tr>
<td>Inside Body</td>
</tr>
</tbody>
</table>
{% endblock %}