3

NOTE: I have no idea how the other post sited answers this question. I did discover the following:

if you want to print to the browser you can just add:

return jsonify(request.form)

if you want to print to the console you can add:

my_data = request.form
for key in my_data:
    print ('form key '+key+" "+my_data[key])
return render_template("some.html")

I'm trying to print all POST variables with the following controller:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/register", methods=["POST"])
def register():  

dict = request.form
for key in dict:
    print ('form key '+dict[key])

but am getting the error:

ValueError: View function did not return a response

here is my form:

{% extends "layout.html" %}

{% block title %}
Frosh IMs
{% endblock %}

{% block body %}
<h1>Register for Frosh IMs</h1>
<form action="{{ url_for('register') }}" method="post">
    Name: <input name="name" type="text"/>
    <br/>
    Dorm:
    <select name="dorm">
      <option value=""></option>
      <option value="Apley Court">Apley Court</option>
      <option value="Canaday">Canaday</option>
      <option value="Grays">Grays</option>
      <option value="Greenough">Greenough</option>
      <option value="Hollis">Hollis</option>
      <option value="Holworthy">Holworthy</option>
      <option value="Hurlbut">Hurlbut</option>
      <option value="Lionel">Lionel</option>
      <option value="Matthews">Matthews</option>
      <option value="Mower">Mower</option>
      <option value="Pennypacker">Pennypacker</option>
      <option value="Stoughton">Stoughton</option>
      <option value="Straus">Straus</option>
      <option value="Thayer">Thayer</option>
      <option value="Weld">Weld</option>
      <option value="Wigglesworth">Wigglesworth</option>
    </select>
    <br/>
    <input type="submit" value="Register"/>
</form>
{% endblock %}
DCR
  • 14,737
  • 12
  • 52
  • 115

1 Answers1

6

you need flask to return something to the browser:

from flask import jsonify

@app.route("/register", methods=["POST"])
def register():
    # this line goes to the console/terminal in flask dev server
    print request.form
    # this line prints out the form to the browser
    return jsonify(request.form.to_dict())

Multidict to_dict() method returns a normal python dict from the MultiDict.

abigperson
  • 5,252
  • 3
  • 22
  • 25
  • thanks, we are getting there. If I just add the line return jsonify(request.form.to_dict()) it prints out the POST variables. why don't I need the print statement, and shouldn't the print request.form come after the jsonify statement? – DCR Mar 02 '17 at 23:53
  • Flask is a web framework. It is the "server" side of a communication with a client (e.g. a web browser). `print` will write the text to [server stdout](http://stackoverflow.com/questions/3385201/confused-about-stdin-stdout-and-stderr) which, if you are running the flask dev server from a terminal, will be the terminal window. once a view function uses `return` any lines after that will be ignored. you should consider reviewing some tutorials as this is really the basics of what flask is and does and how HTTP works. – abigperson Mar 02 '17 at 23:59
  • Thanks, I got that working now. Is there a way to control the printing to the web browser like PHP's print_r function? – DCR Mar 03 '17 at 00:40
  • you would need to use a jinja template (e.g. `return render_template('page.html', data=request.form.to_dict())` instead of `jsonify()`) and use the Jinja2 [pprint filter](http://jinja.pocoo.org/docs/dev/templates/#pprint) in your template like `{{ data|pprint }}` – abigperson Mar 03 '17 at 06:17