카테고리 없음

스파르타 AI-8기 TIL(9/21)

kimjunki-8 2024. 9. 21. 18:18

starting with form

Ex.

<form action="/submit" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
  
  <label for="email">Email:</label>
  <input type="email" id="email" name="email">
  
  <button type="submit">Submit</button>
</form>

<form></form> -> Send the user's input data to the server

action = ->Specifies the URL to which the data will be sent (/submit is the server's endpoint to process the data).

method 

method: Specifies the HTTP transfer method. GET and POST are mainly used.
GET: Sends data as a query string in the URL.
POST: Sends data as the body of the HTTP request.

 

Ex. GET (receive as request.args)
HTML

<form action="/search" method="GET">
  <input type="text" name="query" placeholder="Search">
  <button type="submit">Search</button>
</form>

 

Flask

from flask import Flask, request

app = Flask(__name__)

@app.route('/search')
def search():
    query = request.args.get('query')  # Get 'query' parameter from URL
    return f"Search results for: {query}"

if __name__ == '__main__':
    app.run()

 

Ex. POST -> can close to the data thru request.form

HTML

<form action="/submit" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
  
  <label for="email">Email:</label>
  <input type="email" id="email" name="email">
  
  <button type="submit">Submit</button>
</form>

 

Flask

from flask import Flask, request

app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit():
    name = request.form.get('name')  # Getting the value of the 'name' field in a form
    email = request.form.get('email')  # Getting the value of the 'email' field in a form
    return f"Name: {name}, Email: {email}"

if __name__ == '__main__':
    app.run()

 

In f"Name: {name}, Email: {email}", **f** indicates that you can use curly brackets {} inside the string to insert variables at that location.

request.form.get('name'): Gets the data submitted in the POST request from the form tags.
methods=['POST']:  This endpoint only handles POST requests.

 

Local Development Environment -> just for development

Could Server -> to share the link

 

Bash environment

1. Must make the virtual environment

python -m venv venv  -> makes the virtual environment name venv

2. Activate the VE

source venv/bin/activate

3. Put the list prepared

pip install -r requirements.txt

 

SQL -> language to talk with database = query