๐Ÿผ ๋ฐฑ์•ค๋“œ/Flask

Flask์—์„œ ํ…œํ”Œ๋ฆฟ ์‚ฌ์šฉํ•˜๊ธฐ

๊ณ„๋ž€์†Œ๋…„ 2025. 3. 29. 21:06

Flask๋Š” ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜์—ฌ ์›น ํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ์€ HTML ํŒŒ์ผ๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๊ทธ ์•ˆ์—์„œ ํŒŒ์ด์ฌ ์ฝ”๋“œ(Jinja2 ํ…œํ”Œ๋ฆฟ ์–ธ์–ด)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์ ์œผ๋กœ ์‚ฝ์ž…ํ•˜๊ณ  ์ œ์–ด

 

1. ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ

 

/project_name
    |-- app.py
    |-- config.py
    |-- models.py
    |-- templates/
        |-- categories.html
        |-- products.html
        |-- users.html
    |-- test.db

 

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home Page</title>
</head>
<body>
    <h1>Welcome to the Home Page</h1>
    <p>Click below to see the categories, products, or users:</p>
    <ul>
        <li><a href="/get_categories">View Categories</a></li>
        <li><a href="/get_products">View Products</a></li>
        <li><a href="/get_users">View Users</a></li>
    </ul>
</body>
</html>

 

templates/categories.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Categories</title>
</head>
<body>
    <h1>Categories</h1>
    {% if categories %}
        <ul>
            {% for category in categories %}
                <li>{{ category.name }}</li>
            {% endfor %}
        </ul>
    {% else %}
        <p>No categories available.</p>
    {% endif %}
</body>
</html>

 

templates/products.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Products</title>
</head>
<body>
    <h1>Products</h1>
    {% if products %}
        <ul>
            {% for product in products %}
                <li>{{ product.name }} - ${{ product.price }} (Category: {{ product.category.name }})</li>
            {% endfor %}
        </ul>
    {% else %}
        <p>No products available.</p>
    {% endif %}
</body>
</html>

 

templates/users.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Users</title>
</head>
<body>
    <h1>Users</h1>
    {% if users %}
        <ul>
            {% for user in users %}
                <li>{{ user.username }} - {{ user.email }}</li>
            {% endfor %}
        </ul>
    {% else %}
        <p>No users available.</p>
    {% endif %}
</body>
</html>

 

app.py

from flask import Flask, jsonify, render_template
from models import db, Product, User, Category
import config
from flask_migrate import Migrate

app = Flask(__name__)
app.config.from_object(config)

# ORM ์ดˆ๊ธฐํ™”
db.init_app(app)

# Flask-Migrate ์ดˆ๊ธฐํ™”
migrate = Migrate(app, db)


@app.route('/')
def index():
    # ๊ธฐ๋ณธ ๋ฉ”์ธ ํŽ˜์ด์ง€ ๋ Œ๋”๋ง
    return render_template('index.html')

@app.route('/add_category')
def add_category():
    # ์ƒˆ๋กœ์šด Category ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  DB์— ์ถ”๊ฐ€
    new_category = Category(name="Electronics")
    db.session.add(new_category)
    db.session.commit()
    return "Category added!"

@app.route('/add_product')
def add_product():
    # ์ƒˆ๋กœ์šด Product ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  DB์— ์ถ”๊ฐ€
    # ์ด๋ฏธ ์กด์žฌํ•˜๋Š” Category ID๋ฅผ ํ• ๋‹นํ•˜์—ฌ Product์— ์—ฐ๊ฒฐ
    category = Category.query.first()  # ์ฒซ ๋ฒˆ์งธ ์นดํ…Œ๊ณ ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ (์˜ˆ์‹œ๋กœ)
    new_product = Product(name="Laptop", price=999.99, category_id=category.id)
    db.session.add(new_product)
    db.session.commit()
    return "Product added!"



@app.route('/add_user')
def add_user():
    # ์ƒˆ๋กœ์šด User ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  DB์— ์ถ”๊ฐ€
    new_user = User(username="sample_user", email="user@example.com")
    db.session.add(new_user)
    db.session.commit()
    return "User added!"


@app.route('/get_categories')
def get_categories():
    # DB์—์„œ ๋ชจ๋“  Category ์กฐํšŒ
    categories = Category.query.all()
    return render_template('categories.html', categories=categories)

@app.route('/get_products')
def get_products():
    # DB์—์„œ ๋ชจ๋“  Product ์กฐํšŒ
    products = Product.query.all()
    return render_template('products.html', products=products)


@app.route('/get_users')
def get_users():
    # DB์—์„œ ๋ชจ๋“  User ์กฐํšŒ
    users = User.query.all()
    return render_template('users.html', users=users)



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

๊ธฐ์กด ๋ฐฉ์‹

  • ๋ชจ๋“  ๋ผ์šฐํŠธ์™€ ๊ธฐ๋Šฅ์ด app.py ํ•œ ๊ณณ์— ๋ชจ์—ฌ ์žˆ์—ˆ๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค CRUD ์ž‘์—…)๊ณผ ํ…œํ”Œ๋ฆฟ ๋ Œ๋”๋ง์ด ๋ชจ๋‘ ํ˜ผํ•ฉ ๋จ

๋ณ€๊ฒฝ๋œ ๋ฐฉ์‹

  1. routes ํด๋”: CRUD ์ž‘์—…์„ ๋‹ค๋ฃจ๋Š” ๋ผ์šฐํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํด๋”
    • ์—ฌ๊ธฐ์„œ ์ฃผ๋กœ API ๊ด€๋ จ ๋ผ์šฐํŠธ๋“ค์ด ์ •์˜ ๋จ
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ Product, Category, User ๋ชจ๋ธ์— ๋Œ€ํ•œ CRUD
    • ex) /api/get_products, /api/add_product, /api/update_category
  2. views ํด๋”: ํ…œํ”Œ๋ฆฟ ๋ Œ๋”๋ง๊ณผ ๊ด€๋ จ๋œ ๋ผ์šฐํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํด๋”
    • ์ฃผ๋กœ GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌ, HTML ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ์—ญํ• 
    • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ „๋‹ฌํ•˜์—ฌ UI๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์—ญํ• 
    • ex) /get_categories (์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜์—ฌ HTML๋กœ ๋ Œ๋”๋ง)

 

https://github.com/seongjju/flask/tree/main/views

https://github.com/seongjju/flask/tree/main/routes

 

flask/routes at main · seongjju/flask

flask-practice. Contribute to seongjju/flask development by creating an account on GitHub.

github.com