LoginSignup
0
1

More than 3 years have passed since last update.

【はじめてのFlask】会員制の掲示板を作ってみる。

Posted at

概要

クジラ飛行机さんの PythonではじめるWebサービス&スマホアプリの書きかた・作りかた を読んで、Flaskっていいな、Webサービスっていいなってなったので、せっかくなので自分でも試行錯誤しながら会員制の掲示板を作ってみることにした。
タイトル【はじめてのFlask】って書いてますが、Webサービス開発自体初めてです。

ファイルの構成

my_bbs/
 ├ app.py
 └ templates/
   ├ index.html
   └ login.html

とりあえずURLルーティング

メインのapp.pyにURLルーティングの部分だけ書いてみる。
これでhtmlファイルを2つ作ってから、python app.py してブラウザからそれぞれのURLにアクセスしてURLがちゃんと回るかを確認する。

from flask import Flask, render_template, url_for, redirect

app = Flask(__name__)

@app.route('/login')
def login():
    return render_template('login.html')

@app.route('/try_login')
def try_login():
    return redirect(url_for('index'))

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/write')
def write():
    return redirect(url_for('index'))

@app.route('/logout')
def logout():
    return redirect(url_for('login'))


if __name__ == "__main__":
    app.run(debug=True, host='127.0.0.1')

index.htmlは

<!DOCTYPE html>
<html>
<head>
    <title>
        会員制の掲示板
    </title>
</head>
<body>
    会員制の掲示板 <br>
    テストページ
</body>
</html>

login.htmlは

<!DOCTYPE html>
<html>
<head>
    <title>会員制の掲示板のログインページ</title>
</head>
<body>
    ログインページ
</body>
</html>

ほんとにガワだけって感じでひとまずOK。

Gitでバージョン管理も始める。

$git init 
$git add *
$git commit -m 'first commit'
$git push origin master

ToDo

ひとまずURLは回ったので、次は//write の中身を書いていく。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1