Python Private Lessons / Series 5

in #utopian-io7 years ago (edited)

What will we learn?

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



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]

()

For the rest of the article, follow our series.



Series :

1 - Python Private Lessons / Series 1 #1

2 - Python Private Lessons / Series 2 #2

3 - Python Private Lessons / Series 3 #3

4 - Python Private Lessons / Series 4 #4

5 - Python Private Lessons / Series 5 #5



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @kingmaggot I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

1UP-Kayrex_tiny.png

You've got upvoted by Utopian-1UP!

You can give up to ten 1UP's to Utopian posts every day after they are accepted by a Utopian moderator and before they are upvoted by the official @utopian-io account. Install the @steem-plus browser extension to use 1UP. By following the 1UP-trail using SteemAuto you support great Utopian authors and earn high curation rewards at the same time.


1UP is neither organized nor endorsed by Utopian.io!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63549.46
ETH 2562.53
USDT 1.00
SBD 2.66