Home > Article > Backend Development > How to Access POST and GET Variables in Python?
Manipulating POST and GET Variables in Python
Unlike PHP's simplified handling of POST and GET variables, Python requires the use of frameworks or libraries for seamless data retrieval.
Raw CGI
For basic handling, employ the import cgi module:
<code class="python">import cgi form = cgi.FieldStorage() print form["username"]</code>
Django, Pylons, Flask, or Pyramid
These frameworks offer specialized request objects:
<code class="python">print request.GET['username'] # GET form method print request.POST['username'] # POST form method</code>
Turbogears, Cherrypy
In Turbogears and Cherrypy, utilize the request.params attribute:
<code class="python">from cherrypy import request print request.params['username']</code>
Web.py
Web.py employs the input function:
<code class="python">form = web.input() print form.username</code>
Werkzeug
Werkzeug provides the form attribute:
<code class="python">print request.form['username']</code>
Cherrypy and Turbogears Custom Handlers
Declare handler functions with parameter placeholders:
<code class="python">def index(self, username): print username</code>
Google App Engine
Within a handler class, use the get method:
<code class="python">class SomeHandler(webapp2.RequestHandler): def post(self): name = self.request.get('username') self.response.write(name)</code>
Framework Considerations
Depending on your application requirements, select a suitable framework or library to facilitate efficient handling of POST and GET variables.
The above is the detailed content of How to Access POST and GET Variables in Python?. For more information, please follow other related articles on the PHP Chinese website!