Pythonのdatetimeモジュールについて

flaskを用いて家計簿のようなものを作成しようとしたのですが、datetimeモジュールが思うように動きません。

プログラミング言語はpython,アプリケーションを作るためにflaskを使用し、データベース操作のために
pythonのormであるflask-sqlalchemyを使っています。

アプリケーションの概要ですが、まず、日用品や交際費などのようにジャンル別にカテゴリーを作成し、カテゴリーごとの予算を設定します。お金を使うごとに何にお金をどれくらい使ったかを記録し、それらの情報をウェブページ上に載せるというものです。なお一番下にpythonのコードすべてを載せています。
(HTMLは割愛)

datetimeを用いて、お金を使った日をデータとして保存し、ウェブ上に表示させたいのですが、日付が変わると自動的に時間が更新され、すべての日付がウェブページを開いた日に変わってしまいます。
日付をデータベースに追加した日のまま固定する方法はありますでしょうか。

python

from flask import Flask from flask import render_template,redirect,request from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate import datetime app = Flask(__name__)app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Falsedb = SQLAlchemy(app)migrate = Migrate(app,db) today = datetime.date.today() class Category(db.Model): name = db.Column(db.String,db.ForeignKey('detail.id'),primary_key=True) budget = db.Column(db.Integer,nullable=False) remaining_budget = db.Column(db.Integer,nullable=False) month = db.Column(db.Integer,nullable=False) class Detail(db.Model): id = db.Column(db.Integer,primary_key=True) name = db.relationship('Category',backref='detail',lazy=True) used_money = db.Column(db.Integer,nullable=False) purchased_item = db.Column(db.String,nullable=False) month = db.Column(db.Integer,nullable=False) date = db.Column(db.String,nullable=False) @app.route('/')def index(): print(today.month) display_categorys = Category.query.filter_by(month = 6).all() display_details = Detail.query.filter_by(month = 6).all() index_month = today.month index_day = today.day return render_template('index.html',categorys=display_categorys,details=display_details,this_month=index_month,day=index_day) @app.route('/create_category' ,methods=['GET','POST'])def create_category(): if request.method == 'GET': return render_template('create_category.html') else: category_name = request.form.get('category_name') budget = request.form.get('budget') category_month = today.month add_category = Category(name=category_name,budget=budget,remaining_budget=budget,month=category_month) db.session.add(add_category) db.session.commit() return redirect('/') @app.route('/<string:category_name>/create_detail/' ,methods=['GET','POST'])def create_detail(category_name): check_category = Category.query.filter_by(name=category_name).first() if request.method == 'GET': return render_template('create_detail.html',category=check_category) else: purchased_item = request.form.get('purchased_item') used_money = int(request.form.get('used_money')) check_category.remaining_budget -=used_money detail_day = today.day detail_month = today.month add_detail = Detail(purchased_item=purchased_item,used_money=used_money,date=detail_day,month=detail_month) db.session.add(add_detail) db.session.commit() return redirect('/')

コメントを投稿

0 コメント