Pythonで家計簿アプリを作っているのですが、ポストメソッドで取得した値(数字)がInt型に直すことができずに困っています。
具体的には(下にpythonと一部のhtmlのコードを載せています)
python
1 used_money = request.form.get('used_money') 2 used_money = int(used_money) 3 check_category.remaining_budget -=used_money 4
この部分なんですが、
used_moneyをInt()で囲むと
invalid literal for int() with base 10:
とエラーが出てきました。これはIntに直せないものを直そうとしているので出るエラーだと分かったのですが、値自体はターミナルにprintしてみたところ数字がでてきており、なぜ直らないのか分かりません。
そこでこの値をInt型に直す方法と、今のままでなぜIntに直らないのか、の二点教えていただきたいです。
python
1 2from flask import Flask 3from flask import render_template,redirect,request 4from flask_sqlalchemy import SQLAlchemy 5from flask_migrate import Migrate 6import datetime 7 8app = Flask(__name__) 9app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' 10app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 11db = SQLAlchemy(app) 12migrate = Migrate(app,db) 13 14class Category(db.Model): 15 category_name = db.Column(db.String,db.ForeignKey('detail.id'),primary_key=True) 16 budget = db.Column(db.Integer,nullable=False) 17 remaining_budget = db.Column(db.Integer,nullable=False) 18 month = db.Column(db.Integer,nullable=False) 19 20class Detail(db.Model): 21 id = db.Column(db.Integer,primary_key=True) 22 category_name = db.relationship('Category',backref='detail',lazy=True) 23 used_money = db.Column(db.Integer,nullable=False) 24 purchased_item = db.Column(db.String,nullable=False) 25 month = db.Column(db.Integer,nullable=False) 26 day = db.Column(db.Integer,nullable=False) 27 28today = datetime.date.today() 29missing_entry = 0 30duplicate_error = 0 31 32@app.route('/',methods=['GET','POST']) 33def index(): 34 if request.method == 'GET': 35 display_categorys = Category.query.filter(Category.month == 6).all() 36 display_details = Detail.query.filter(Detail.month == 6).all() 37 check_count = Detail.query.filter(Detail.purchased_item.like('%eh%')).count() 38 month = today.month 39 if check_count == 0: 40 return render_template('index.html',categorys=display_categorys,details=display_details,month=month,count=0) 41 else: 42 return render_template('index.html',categorys=display_categorys,details=display_details,month=month) 43 else: 44 month = request.get('month') 45 try: 46 search_categorys = Category.query.fileter_by(month = month).all() 47 search_details = Detail.query.filter_by(month = month).all() 48 return render_template('index.html',categorys=search_categorys,details=search_details,month=month) 49 except: 50 return render_template('index.html') 51 52@app.route('/create_category' ,methods=['GET','POST']) 53def create_category(): 54 if request.method == 'GET': 55 return render_template('create_category.html') 56 else: 57 category_name = request.form.get('category_name') 58 budget = request.form.get('budget') 59 category_month = today.month 60 add_category = Category(category_name=category_name,budget=budget,remaining_budget=budget,month=category_month) 61 db.session.add(add_category) 62 try: 63 db.session.commit() 64 return redirect('/') 65 except: 66 if category_name == '' or budget == '': 67 return render_template('create_category.html' ,missing_entry = 1) 68 else: 69 return render_template('create_category.html',duplicate_error = 1) 70 71@app.route('/<string:category_name>/create_detail/' ,methods=['GET','POST']) 72def create_detail(category_name): 73 check_category = Category.query.filter_by(category_name=category_name).first() 74 if request.method == 'GET': 75 return render_template('create_detail.html',category=check_category) 76 else: 77 purchased_item = request.form.get('purchased_item') 78 used_money = request.form.get('used_money') 79 check_category.remaining_budget -=used_money 80 detail_day = today.day 81 detail_month = today.month 82 try: 83 add_detail = Detail(purchased_item=purchased_item,used_money=used_money,day=detail_day,month=detail_month) 84 db.session.add(add_detail) 85 db.session.commit() 86 return redirect('/') 87 except: 88 return render_template('create_detail.html',missing_entry = 1) 89
html
1{% extends 'base.html' %} 2{% block body %} 3<h1 class="title">{{month}}月の家計簿</h1> 4<h1 class="detail_second_title">帳簿</h1> 5<h2 class="detail_category_name">カテゴリ:{{category.category_name}}</h2> 6{% if error == 1 %} 7<h3>記入漏れ、または記入エラーがあります</h3> 8{% endif %} 9<article class="detail_form"> 10 <form action="" method="POST"> 11 <label for="purchased_item" class="detail_label_purchased_money"><h3>買ったもの</h3></label> 12 <input type="text" name="purchased_item" class="detail_input_purchased_item"> 13 <label for="used_money"><h3 class="detail_label_used_money">金額</h3></label> 14 <input type="number" name="used_money" class="detail_input_used_money"> 15 <input type="submit" value="確定" class="detail_input_submit"> 16 </form> 17</article> 18{% endblock %}
回答2件