Python Private Lessons / Series 5

By @kingmaggot2/22/2018utopian-io

What will we learn?

  • Python Private Lessons / Series 4 ( Using URL links )

https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Python_logo_and_wordmark.svg/2000px-Python_logo_and_wordmark.svg.png


Requirements:

  • Notepad+
  • Operating System
  • Python

Difficulty Level:

  • Normal Level

Let's Start the Course :

While clicking on a site, we can create some variables in the URL structure and populate those variables. This type of URL is called sending data via the "GET" method.

To access the variables on the right side of the question mark we need to add the "CGI" module in our Python file. We will use this module frequently.

import cgi

Create a new variable with the "FieldStorage ()" method in the "Cgi" module.

x = cgi.FieldStorage()

Now we can use the getFirst () method in the variable "x" to get the value of the variable in the address line.

Name = x.getfirst("name")
Surname = x.getfirst("surname")
Gender = x.getfirst("gender")

print ("""
<strong> Name : </strong> """ + name + """ <br>
<strong> Surname : </strong> """ + surname + """ <br>
<strong> Gender: </strong> """ + gender + """ <br>
	""")

See all the Codes again.

#!/usr/bin/python

import cgi

# Turn on debug mode.
import cgitb
cgitb.enable()

# Print necessary headers.
print("Content-Type: text/html")
print()


x = cgi.FieldStorage ()

Name = x.getfirst("name")
Surname = x.getfirst("surname")
Gender = x.getfirst("gender")

print ("""
<strong> Name : </strong> """ + name + """ <br>
<strong> Surname : </strong> """ + surname + """ <br>
<strong> Gender: </strong> """ + gender + """ <br>
	""")

If we return null, we can specify in the second parameter the value to be assigned by default.

x.getfirst ("Gender", "Unspecified")

In such a use, if there is no gender data, it will return to us as "Unspecified". In the next section you will find a more detailed example of the process.

Form Operations

The form structure is used to retrieve data from internet pages and handle them dynamically. For example, we usually use the form structure when performing operations such as member entry, member registration, membership settings, ordering from e-commerce sites, file uploading. The form structure is a feature that will surely come into conflict. Let's make a member registration form using a form structure.

In the previous section, we performed the operation of the data sent with the "get" method. Now we will process the data sent by "POST" method. First, let's prepare a form with HTML codes. Keep these codes in the form.html file.

<style>
     input {
                    padding: 15px;
                    height: auto;
                    display: block;
                    margin-bottom: 10px;
                    width: 400px;
                    font-size: 14px;
                    background: #f9f9f9;
                    border: soild 1px #d9d9d9;
        }
         button{
                    padding: 10px 15px;
                    border: soild 1px #d9d9d9;
                    border-radius: 4px;
                    background: #67B031;
                    color: #fff;
</style>

<form action="send.py" method="post">
     <input type="text" name="name" placeholder="Write Your Name">
     <input type="text" name="surname" placeholder="Write Surname">
     <input type="text" name="email" placeholder="Write Email Address">

    <button type="submit">SEND</button>
</form>

We prepared the form and gave it a nice style.

1.png

As we indicated in the action parameter in the "

" tag, when we click on the "SEND" button, this forum will send the data we received from us directly to the "send.py" file. We will take this data in that friend and use it as we wish.

The method parameter in the "" tag takes two values ​​as "POST" or "GET". We wrote "POST" to where we want the data to be sent according to "POST" method. If we had set it to "GET", this address would be placed on the address line.

Now we create a file called "send.py" and print the data we sent with the form. Do not forget to set the "chmod" setting for this file to 755, otherwise your file will not work.

Again, we need to include the "Cgi" module in the project. Then we need to create a variable with the "feildStorage ()" method in the cgi module as we created in the previous section. We used the "getFirst ()" function when retrieving data with the "GET" method. This time we will use the "getvalue ()" function to draw data with the "POST" method.

x = cgi.FieldStorage ()
print (x.getvalue ("Name"))

In this way, we get the value of the tag with the "name" parameter "name" in the HTML code in use. If we return null, we can specify in the second parameter the value to be assigned by default.

x.getvalue ("Name" , "NO")

In such a usage, this function will automatically return a value of "NO" if the form element named "name" has an empty value.

Let's first print the data we get from the form without using the second parameter.

#! /usr/bin/python

import cgi

# Turn on debug mode.
import cgitb
cgitb.enable()

# Print necessary headers.
print("Content-Type: text/html")

print()

x = cgi.FieldStorage ()

name = x.getfirst("name")
surname = x.getfirst("surname")
email = x.getfirst("email")

print ("""
<strong> name : </strong> """ + str(name) + """ <br>
<strong> surname : </strong> """ + str(surname) + """ <br>
<strong> email: </strong> """ + str(email) + """ <br>
	""")

![2.png]

(https://res.cloudinary.com/hpiynhbhq/image/upload/v1519332999/eo6dcvmfwfny5iblgpyt.png)

For the rest of the article, follow our series.


**Series :**
1 - [Python Private Lessons / Series 1](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-1) #1
2 - [Python Private Lessons / Series 2](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-2) #2
3 - [Python Private Lessons / Series 3](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-3) #3
4 - [Python Private Lessons / Series 4](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-4) #4
5 - [Python Private Lessons / Series 5](https://utopian.io/utopian-io/@kingmaggot/python-private-lessons-series-5) #5



Posted on Utopian.io - Rewarding Open Source Contributors

5

comments