flaskを用いて家計簿のようなものを作成しようとしたのですが、datetimeモジュールが思うように動きません。
プログラミング言語はpython,アプリケーションを作るためにflaskを使用し、データベース操作のために
pythonのormであるflask-sqlalchemyを使っています。
アプリケーションの概要ですが、まず、日用品や交際費などのようにジャンル別にカテゴリーを作成し、カテゴリーごとの予算を設定します。お金を使うごとに何にお金をどれくらい使ったかを記録し、それらの情報をウェブページ上に載せるというものです。なお一番下にpythonのコードすべてを載せています。
(HTMLは割愛)
datetimeを用いて、お金を使った日をデータとして保存し、ウェブ上に表示させたいのですが、日付が変わると自動的に時間が更新され、すべての日付がウェブページを開いた日に変わってしまいます。
日付をデータベースに追加した日のまま固定する方法はありますでしょうか。
python
1from flask import Flask 2from flask import render_template,redirect,request 3from flask_sqlalchemy import SQLAlchemy 4from flask_migrate import Migrate 5import datetime 6 7app = Flask(__name__) 8app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' 9app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 10db = SQLAlchemy(app) 11migrate = Migrate(app,db) 12 13today = datetime.date.today() 14 15class Category(db.Model): 16 name = db.Column(db.String,db.ForeignKey('detail.id'),primary_key=True) 17 budget = db.Column(db.Integer,nullable=False) 18 remaining_budget = db.Column(db.Integer,nullable=False) 19 month = db.Column(db.Integer,nullable=False) 20 21class Detail(db.Model): 22 id = db.Column(db.Integer,primary_key=True) 23 name = db.relationship('Category',backref='detail',lazy=True) 24 used_money = db.Column(db.Integer,nullable=False) 25 purchased_item = db.Column(db.String,nullable=False) 26 month = db.Column(db.Integer,nullable=False) 27 date = db.Column(db.String,nullable=False) 28 29@app.route('/') 30def index(): 31 print(today.month) 32 display_categorys = Category.query.filter_by(month = 6).all() 33 display_details = Detail.query.filter_by(month = 6).all() 34 index_month = today.month 35 index_day = today.day 36 return render_template('index.html',categorys=display_categorys,details=display_details,this_month=index_month,day=index_day) 37 38 39@app.route('/create_category' ,methods=['GET','POST']) 40def create_category(): 41 if request.method == 'GET': 42 return render_template('create_category.html') 43 else: 44 category_name = request.form.get('category_name') 45 budget = request.form.get('budget') 46 category_month = today.month 47 add_category = Category(name=category_name,budget=budget,remaining_budget=budget,month=category_month) 48 db.session.add(add_category) 49 db.session.commit() 50 return redirect('/') 51 52@app.route('/<string:category_name>/create_detail/' ,methods=['GET','POST']) 53def create_detail(category_name): 54 check_category = Category.query.filter_by(name=category_name).first() 55 if request.method == 'GET': 56 return render_template('create_detail.html',category=check_category) 57 else: 58 purchased_item = request.form.get('purchased_item') 59 used_money = int(request.form.get('used_money')) 60 check_category.remaining_budget -=used_money 61 detail_day = today.day 62 detail_month = today.month 63 add_detail = Detail(purchased_item=purchased_item,used_money=used_money,date=detail_day,month=detail_month) 64 db.session.add(add_detail) 65 db.session.commit() 66 return redirect('/')
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/28 07:41