3

Im trying to get my button to redirect to a route in my program with some variable values to dynamically generate a pdf, the pdf generation works by itself but when i submit the form instead of going to genreport/{{lid}/{{userID}} i go to genreport//?userID=3

The code for my form

<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>

The code for my route

@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

Any help would be greatly appreciated as i have spent a good amount of time on this issue. If any more information is needed i can provide it.

EDIT

Here are all the routes of my application with the changes i have made since i posted the question

@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
1
  • @trevor add the template for that render your form Commented Nov 18, 2017 at 9:24

2 Answers 2

7

If you using parametrizing url you can use function with parameters:

# 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']

Second option use url parameters, you should use request.args.get:

# url to call will be:
# genreport?lid=1&userID=2
@app.route('/genreport')
def pdf_template():
    lid = request.args.get('lid')
    userID = request.args.get('userID')
    print(lid, userID)
Sign up to request clarification or add additional context in comments.

1 Comment

The second option was the one that worked and it worked beautifully so thank you for all the help I really appreciate it!
0

The solution is to use url_for to build urls for your forms

what you need in your form is:

<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>

and the route :

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.