Я пытаюсь назначить некоторые объекты JSON в мою базу данных PostgreSQL, которая называется «минипроект», а таблица, в которую я пытаюсь вставить, называется «мини_проект», но она всегда возвращает исключение «KeyError: None» какследует:
Полезно знать, что:
application.py
from flask import Flask, render_template, request
from flask_api import FlaskAPI
from models import *
app = Flask(__name__)
app = FlaskAPI(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = ("postgresql://postgres:password@localhost/miniproject")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
@app.route("/apod", methods=["POST"])
def pic():
# Get form information.
date = request.form.get("date")
res = requests.get("https://api.nasa.gov/planetary/apod?api_key=some_key",
params={"date": date})
if res.status_code != 200:
raise Exception(f"{res.status_code} - ERROR: API request unsuccessful.")
else:
data = res.json()
explanation = data ["explanation"]
url = data ["url"]
copyrights = data ["copyright"]
title = data ["title"]
miniproject.add_to(date, url, explanation, copyrights, title)
return render_template("index.html", url= url, explanation= explanation, copyrights= copyrights, title=title)
models.py
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class miniproject(db.Model):
__tablename__ = "mini_project"
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DATE, nullable=False)
url = db.Column(db.String, nullable=False)
explanation = db.Column(db.String, nullable=True)
copyrights = db.Column(db.String, nullable=False)
title = db.Column(db.String, nullable=False)
def add_to(self, date, url, explanation, copyrights, title):
m = miniproject(date= date, url=url, explanation=explanation,
copyrights= copyrights, title=title)
db.session.add(m)
db.session.commit()
Состояние ошибки:
KeyError
KeyError: Нет
Трассировка (последний последний вызов)
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\Courses\Very Important\CS50 Web programming with Python and JavaScript\miniProject\miniProject.py", line 39, in pic
miniproject.add_to(date)
During handling of the above exception, another exception occurred:
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\users\msi pc\appdata\local\programs\python\python37-32\lib\site-packages\flask_api\app.py", line 96, in handle_user_exception
app_handlers = self.error_handler_spec[None].get(None, ())
KeyError: None