Home  >  Article  >  Backend Development  >  How do I handle POST and GET variables in Python using different web frameworks?

How do I handle POST and GET variables in Python using different web frameworks?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-30 14:41:55316browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn