Skip to main content

Incoming!

 Incoming!

Today, we're going to learn how to deal with data from forms in Flask.


πŸ‘‰ To start, I've added yesterday's HTML code for my form in main.py for you already. (You're welcome!) Go take a look!


πŸ‘‰ However, at the moment, the app.route() has no method associated with it, so I need to create a route for this page to receive the data.


First, I need a new import: request.


Then I create the app.route - I also need to add an extra argument to specify the methods being received. At the moment, that's just 'post', but it does need to be ALL CAPS - POST.


Finally I define the process() subroutine that returns request.form


πŸ‘‰ Here's the new code on its own:


from Flask import Flask, request

app.route('/process', methods=["POST"])

def process():

  return request.form



πŸ‘‰ And here it is as part of the whole code:


from flask import Flask, request

app = Flask(__name__)

app.route("/process", methods=["POST"])

def process():

  return request.form

  

@app.route('/')

def index():

  page = """<form method = "post" action="/process">

    <p>Name: <input type="text" name="username" required> </p>

    <p>Email: <input type="Email" name="email"> </p>

    <p>Website: <input type="url" name="website"> </p>

    <p>Age: <input type="number" name="age"> </p>

    <input type="hidden" name="userID" value="232"> </p>

    <p>

      Fave Baldy: 

      <select name="baldies">

        <option>David</option>

        <option>Jean Luc Picard</option>

        <option>Yul Brynner</option>

      </select>

    </p>

    <button type="submit">Save Data</button>

  </form>

    

    

    """

  return page

app.run(host='0.0.0.0', port=81)



This will get us the data from the form in a dictionary format, like this:  

 






Comments

Popular posts from this blog

Web Scraping

 Web Scraping Some websites don't have lovely APIs for us to interface with. If we want data from these pages, we have to use a tecnique called scraping. This means downloading the whole webpage and poking at it until we can find the information we want. You're going to use scraping to get the top ten restaurants near you. Get started πŸ‘‰ Go to a website like Yelp and search for the top 10 reastaurants in your location. Copy the URL.   url = "https://www.yelp.co.uk/search?find_desc=Restaurants&find_loc=San+Francisco%2C+CA%2C+United+States"   Import libraries πŸ‘‰ Import your libraries. Beautiful soup is a specialist library for extracting the contents of HTML and helping us parse them. Run the Repl once your imports are sorted because we want the Beautiful Soup library to be installed (it'll run quicker this way). import requests from bs4 import BeautifulSoup url = "https://www.yelp.co.uk/search?find_desc=Restaurants&find_loc=San+Francisco%2C+CA%2C+Unite...

String Manipulation

   String Manipulation Does this code look familiar from the insult generator project? name = input("What's your name? ") if name == "David" or name == "david":   print("Hello Baldy!") else:    print("What a beautiful head of hair!") Right now, if the user writes "DAVID" or "david", the if statement works correctly. However, "DaVID" does not give the correct output. To the computer, " david", "dAviD", and "david" are completely different. To simplify what the user typed in, we can add these functions to the end of the name of the variable: .lower() = all letters are lower case .upper() = all letters are upper case .title() = capital letter for the first letter of every word .capitalize() = capital letter for the first letter of only the first word    

It's Called Hashing,Hashing, Printing the Hash , Salty, Second User ,

 It's Called Hashing One of the big issues with storing usernames and passwords in a database is what happens if we're hacked? If those passwords are stored as text, our users' security is compromised. Probably across multiple sites because they ignored our advice and used the same password for everything!!!!! Hashing  In reality, organizations don't store your actual password. They store a hash of your password. A hash is produced by turning your password into a sequence of numbers, then passing it though a hashing algorithm (some mathematical process that is very difficult to reverse engineer). The data spit out of this hashing algorithm is what's stored instead of your actual password. πŸ‘‰ So let's do it. I'm using the built-in hash function to create a numerical hash of the password  password = "baldy1" password = hash(password) print(password) # This will output a really long number  πŸ‘‰ Now let's store that hashed version in our database in...