我试图让我的按钮重定向到我的程序中的一个路径,其中包含一些变量值以动态生成pdf,pdf生成单独工作,但是当我提交表单而不是转到genreport/{{lid}/{{userID}}
时
我转到genreport//?userID=3
我的表单的代码
<form action="/genreport/{{lid}}/{{userID}}">
<input type="number" placeholder="ID" id="userID" name="userID">
<select>
<option value="1" id="lid" name="lid">{{letter.description}}</option>
</select>
<input type="submit" value="Generate Report">
</form>
</div>
我路线的代码
@app.route('/genreport/<lid>/<userID>')
def pdf_template():
lid = request.form['lid']
userID = request.form['userID']
policy = Policy.query.get(request.form[userID])
letter = Letter.query.get(request.form[lid])
rendered = render_template_string(letter.template, policy=policy, lid=lid, userID=userID)
pdf = pdfkit.from_string(rendered, False)
response = make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'inline; filename=output.pdf'
return response
任何帮助都会非常感激,因为我在这个问题上花了很多时间。如果需要更多信息,我可以提供。
修改
以下是我申请的所有路线以及自我发布问题后所做的更改
@app.route('/')
@login_required
def index():
return render_template('index.html')
@app.route('/reports', methods=['GET','POST'])
@login_required
def reports():
if request.method == "GET":
return render_template('reports.html')
letter = Letter.query.get(1)
first = request.form['searchFirst']
contact = Contact.query.filter_by(first=first).first()
return render_template('results.html', contact=contact, letter=letter)
@app.route('/results', methods=['GET','POST'])
@login_required
def results():
return render_template('results.html')
@app.route('/management')
@login_required
def management():
return render_template('management.html')
@app.route('/history')
@login_required
def history():
return render_template('history.html')
@app.route('/settings')
@login_required
def settings():
return render_template('settings.html')
@app.route('/login', methods=['GET','POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
username = request.form['inputEmail']
password = request.form['inputPassword']
remember_me = False
if 'remember_me' in request.form:
remember_me = True
registered_user = User.query.filter_by(username=username, password=password).first()
if registered_user is None:
flash('Username or Password is invalid', 'error')
return redirect(url_for('login'))
login_user(registered_user, remember=remember_me)
flash('Logged in Successfully')
return redirect(request.args.get('next') or url_for('index'))
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'GET':
return render_template('signup.html')
user = User(request.form['inputFirst'], request.form['inputLast'], request.form['inputEmail'], request.form['dob'], request.form['inputPassword', request.form['inputStreet'],request.form['inputState'],request.form['inputCity'],request.form['inputZip'],request.form['inputPhone']])
db.session.add(user)
db.session.commit()
flash('User successfully created!')
return redirect(url_for('login'))
@app.route('/profile/<name>')
@login_required
def profile():
return render_template('profile.html', name=name)
@app.route('/admin')
@login_required
def admin():
return render_template('admin.html')
@app.route('/genreport/<lid>/<id>')
def pdf_template():
policy = Policy.query.get(request.form['userID'])
letter = Letter.query.get(request.form['lid'])
rendered = render_template_string(letter.template, policy=policy, letter=letter)
pdf = pdfkit.from_string(rendered, False)
response = make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'inline; filename=output.pdf'
return response
答案 0 :(得分:0)
如果使用参数化网址,则可以使用带参数的函数:
public class Product extends ConsoleProgram
{
public void run()
{
int intValue = 5;
double doubleValue = 2.5;
int product1 = product(intValue, intValue);
System.out.println(product1);
// Use method overloading to define methods
// for each of the following method calls
double product2 = product(doubleValue , doubleValue);
System.out.println(product1);
int product3 = product(intValue * intValue, intValue);
System.out.println(product1);
double product4 = product(intValue, doubleValue);
System.out.println(product1);
double product5 = product(doubleValue, intValue);
System.out.println(product1);
double product6 = product(doubleValue *doubleValue, doubleValue);
System.out.println(product1);
}
public int product(int one, int two)
{
return one * two;
}
}
第二个选项使用url参数,你应该使用request.args.get:
# url to call will be:
# genreport/1/2
@app.route('/genreport/<lid>/<userID>')
def pdf_template(lid, userID):
print(lid, userID)
# lid = request.form['lid']
# userID = request.form['userID']
答案 1 :(得分:0)
解决方案是使用url_for 为您的表单构建网址
您在表单中需要的是:
<form action="{{url_for('app.pdf_template', userID=1, lid=1)}}">
<input type="number" placeholder="ID" id="userID" name="userID">
<select>
<option value="1" id="lid" name="lid">{{letter.description}}</option>
</select>
<input type="submit" value="Generate Report">
</form>
</div>
和路线:
@app.route('/genreport/<lid>/<userID>')
def pdf_template(lid, userID):
lid = request.form[lid]
userID = request.form[userID]
policy = Policy.query.get(userId)
letter = Letter.query.get(lid)
rendered = render_template_string(letter.template, policy=policy, lid=lid, userID=userID)
pdf = pdfkit.from_string(rendered, False)
response = make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'inline; filename=output.pdf'
return response