Home > Article > Backend Development > How do I handle POST and GET variables in Python using different web frameworks?
Handling POST and GET Variables in Python
In Python, the handling of POST and GET variables differs depending on the web framework employed.
Raw CGI
For raw CGI, use cgi.FieldStorage() to access POST variables:
<code class="python">import cgi form = cgi.FieldStorage() print(form["username"])</code>
Popular Web Frameworks
Django / Pylons / Flask / Pyramid:
<code class="python">print(request.GET['username']) # GET print(request.POST['username']) # POST</code>
Turbogears / Cherrypy:
<code class="python">from cherrypy import request print(request.params['username'])</code>
Web.py:
<code class="python">form = web.input() print(form.username)</code>
Werkzeug:
<code class="python">print(request.form['username'])</code>
Cherrypy / Turbogears (Alternative)
You can also define handler functions with parameters for direct access to variables:
<code class="python">def index(self, username): print(username)</code>
Google App Engine
In Google App Engine:
<code class="python">class SomeHandler(webapp2.RequestHandler): def post(self): name = self.request.get('username') self.response.write(name)</code>
Selecting a Framework
Ultimately, the choice of web framework will determine the specific syntax for handling POST and GET variables in Python. Consider the specific features and requirements of each framework before making a decision.
The above is the detailed content of How do I handle POST and GET variables in Python using different web frameworks?. For more information, please follow other related articles on the PHP Chinese website!