app not working and there is no errors and i don’t know what’s going on, could you please help me with that i am new with flask

  flask, python, visual-studio
from flask import Flask, render_template, request, redirect, url_for, jsonify, abort
from flask_sqlalchemy import SQLAlchemy
import sys

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://postgres:[email protected]:5432/todoapp'
db = SQLAlchemy(app)

class Todo(db.Model):
  __tablename__ = 'todos'
  id = db.Column(db.Integer, primary_key=True)
  description = db.Column(db.String(), nullable=False)

  def __repr__(self):
    return f'<Todo {self.id} {self.description}>'


@app.route('/todos/create', methods=['POST'])
def create_todo():
  error = False
  body = {}
  try:
    description = request.form['description']
    todo = Todo(description=description)
    db.session.add(todo)
    db.session.commit()
    body['description'] = todo.description
  except:
    error = True
    db.session.rollback()
    print(sys.exc_info())
  finally:
    db.session.close()
  if error:
    abort (400)
  else:
    return jsonify(body)

@app.route('/')
def index():
  return render_template('index.html', data=Todo.query.all())
<html>

<head>
  <title>Todo App</title>
  <style>
    #error {
      display: none;
    }
  </style>
</head>

<body>
  <div id="error" class="hidden">Something went wrong!</div>
  <form id="form" method="post" action="/todos/create">
    <input type="text" id="description" name="description" />
    <input type="submit" value="Create" />
  </form>
  <ul id="todos">
    {% for d in data %}
    <li>{{ d.description }}</li>
    {% endfor %}
  </ul>
  <script>
    const descInput = document.getElementById('description');
    document.getElementById('form').onsubmit = function(e) {
      e.preventDefault();
      const desc = descInput.value;
      descInput.value = '';
      fetch('/todos/create', {
          method: 'POST',
          body: JSON.stringify({
            'description': desc,
          }),
          headers: {
            'Content-Type': 'application/json',
          }
        })
        .then(response => response.json())
        .then(jsonResponse => {
          console.log('response', jsonResponse);
          li = document.createElement('li');
          li.innerText = desc;
          document.getElementById('todos').appendChild(li);
          document.getElementById('error').className = 'hidden';
        })
        .catch(function() {
          document.getElementById('error').className = '';
        })
    }
  </script>
</body>

</html>

enter image description here
i am trying to write anything on the browser and have it stored in the database and be seen on the browser and you can see what i want to do in the picture I added in the link ……………………………………………………………………………………

Source: Python Questions

LEAVE A COMMENT