Update database using Flask, Bootstrap and Ajax












1















I want to update my database (add opinion) and automatically show them on my site without refreshing. I use flask, bootstrap and I want to write script to send data in AJAX.



So, it's my site when I have details about book(detail_book.html):



{% extends "bootstrap/base.html" %}
{% block content %}
*Here I have jinja2 to display content sent by render_template, not necessery to add here*
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">
Add opinion
</button>

<!-- MODAL FORM-->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Your opinion about {{ book.title }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" href="#" data-role="add">Send opinion</button>
</div>
</div>
</div>
</div>
<!-- END MODAL FORM -->
{% endblock %}


So after clicking on button the modal window is displayed. After filling the message and clicking another button, I want to send id of book, author id and opinion to database. My tables in database look like:



class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(40), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
posts = db.relationship('Opinion', backref='author', lazy='dynamic')

class Opinion(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
book_id = db.Column(db.Integer, db.ForeignKey('book.id'))

class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), index=True)
description = db.Column(db.String(255))
author = db.Column(db.String(255))
pages = db.Column(db.Integer)
opinions = db.relationship('Opinion', lazy='dynamic')


And in my *routes.py I have method:



@app.route('/detail_book/<title>')
def detail_book(title):
book = Book.query.filter_by(title=title).first_or_404()
result = db.session.query(Book.id, Opinion.body).filter(Opinion.book_id==book.id).filter(and_(Book.title == title)).all()
return render_template('detail_book.html', book=book, result=result)


So to sum up, I want to get data from my modal, send it to 1 table in database(Opinion) and display this opinion in my endpoint.
I have never worked with AJAX, I tried diffrent scripts from web, but in the end I gave up. Can someone help me solve this problem?










share|improve this question



























    1















    I want to update my database (add opinion) and automatically show them on my site without refreshing. I use flask, bootstrap and I want to write script to send data in AJAX.



    So, it's my site when I have details about book(detail_book.html):



    {% extends "bootstrap/base.html" %}
    {% block content %}
    *Here I have jinja2 to display content sent by render_template, not necessery to add here*
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">
    Add opinion
    </button>

    <!-- MODAL FORM-->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
    <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Your opinion about {{ book.title }}</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
    <span aria-hidden="true">&times;</span>
    </button>
    </div>
    <div class="modal-body">
    <form>
    <div class="form-group">
    <label for="message-text" class="col-form-label">Message:</label>
    <textarea class="form-control" id="message-text"></textarea>
    </div>
    </form>
    </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-primary" href="#" data-role="add">Send opinion</button>
    </div>
    </div>
    </div>
    </div>
    <!-- END MODAL FORM -->
    {% endblock %}


    So after clicking on button the modal window is displayed. After filling the message and clicking another button, I want to send id of book, author id and opinion to database. My tables in database look like:



    class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(40), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    posts = db.relationship('Opinion', backref='author', lazy='dynamic')

    class Opinion(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    book_id = db.Column(db.Integer, db.ForeignKey('book.id'))

    class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), index=True)
    description = db.Column(db.String(255))
    author = db.Column(db.String(255))
    pages = db.Column(db.Integer)
    opinions = db.relationship('Opinion', lazy='dynamic')


    And in my *routes.py I have method:



    @app.route('/detail_book/<title>')
    def detail_book(title):
    book = Book.query.filter_by(title=title).first_or_404()
    result = db.session.query(Book.id, Opinion.body).filter(Opinion.book_id==book.id).filter(and_(Book.title == title)).all()
    return render_template('detail_book.html', book=book, result=result)


    So to sum up, I want to get data from my modal, send it to 1 table in database(Opinion) and display this opinion in my endpoint.
    I have never worked with AJAX, I tried diffrent scripts from web, but in the end I gave up. Can someone help me solve this problem?










    share|improve this question

























      1












      1








      1








      I want to update my database (add opinion) and automatically show them on my site without refreshing. I use flask, bootstrap and I want to write script to send data in AJAX.



      So, it's my site when I have details about book(detail_book.html):



      {% extends "bootstrap/base.html" %}
      {% block content %}
      *Here I have jinja2 to display content sent by render_template, not necessery to add here*
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">
      Add opinion
      </button>

      <!-- MODAL FORM-->
      <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
      <div class="modal-content">
      <div class="modal-header">
      <h5 class="modal-title" id="exampleModalLabel">Your opinion about {{ book.title }}</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
      </button>
      </div>
      <div class="modal-body">
      <form>
      <div class="form-group">
      <label for="message-text" class="col-form-label">Message:</label>
      <textarea class="form-control" id="message-text"></textarea>
      </div>
      </form>
      </div>
      <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      <button type="button" class="btn btn-primary" href="#" data-role="add">Send opinion</button>
      </div>
      </div>
      </div>
      </div>
      <!-- END MODAL FORM -->
      {% endblock %}


      So after clicking on button the modal window is displayed. After filling the message and clicking another button, I want to send id of book, author id and opinion to database. My tables in database look like:



      class User(UserMixin, db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(40), index=True, unique=True)
      email = db.Column(db.String(120), index=True, unique=True)
      password_hash = db.Column(db.String(128))
      posts = db.relationship('Opinion', backref='author', lazy='dynamic')

      class Opinion(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      body = db.Column(db.String(140))
      timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      book_id = db.Column(db.Integer, db.ForeignKey('book.id'))

      class Book(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      title = db.Column(db.String(255), index=True)
      description = db.Column(db.String(255))
      author = db.Column(db.String(255))
      pages = db.Column(db.Integer)
      opinions = db.relationship('Opinion', lazy='dynamic')


      And in my *routes.py I have method:



      @app.route('/detail_book/<title>')
      def detail_book(title):
      book = Book.query.filter_by(title=title).first_or_404()
      result = db.session.query(Book.id, Opinion.body).filter(Opinion.book_id==book.id).filter(and_(Book.title == title)).all()
      return render_template('detail_book.html', book=book, result=result)


      So to sum up, I want to get data from my modal, send it to 1 table in database(Opinion) and display this opinion in my endpoint.
      I have never worked with AJAX, I tried diffrent scripts from web, but in the end I gave up. Can someone help me solve this problem?










      share|improve this question














      I want to update my database (add opinion) and automatically show them on my site without refreshing. I use flask, bootstrap and I want to write script to send data in AJAX.



      So, it's my site when I have details about book(detail_book.html):



      {% extends "bootstrap/base.html" %}
      {% block content %}
      *Here I have jinja2 to display content sent by render_template, not necessery to add here*
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">
      Add opinion
      </button>

      <!-- MODAL FORM-->
      <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
      <div class="modal-content">
      <div class="modal-header">
      <h5 class="modal-title" id="exampleModalLabel">Your opinion about {{ book.title }}</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
      </button>
      </div>
      <div class="modal-body">
      <form>
      <div class="form-group">
      <label for="message-text" class="col-form-label">Message:</label>
      <textarea class="form-control" id="message-text"></textarea>
      </div>
      </form>
      </div>
      <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      <button type="button" class="btn btn-primary" href="#" data-role="add">Send opinion</button>
      </div>
      </div>
      </div>
      </div>
      <!-- END MODAL FORM -->
      {% endblock %}


      So after clicking on button the modal window is displayed. After filling the message and clicking another button, I want to send id of book, author id and opinion to database. My tables in database look like:



      class User(UserMixin, db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(40), index=True, unique=True)
      email = db.Column(db.String(120), index=True, unique=True)
      password_hash = db.Column(db.String(128))
      posts = db.relationship('Opinion', backref='author', lazy='dynamic')

      class Opinion(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      body = db.Column(db.String(140))
      timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      book_id = db.Column(db.Integer, db.ForeignKey('book.id'))

      class Book(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      title = db.Column(db.String(255), index=True)
      description = db.Column(db.String(255))
      author = db.Column(db.String(255))
      pages = db.Column(db.Integer)
      opinions = db.relationship('Opinion', lazy='dynamic')


      And in my *routes.py I have method:



      @app.route('/detail_book/<title>')
      def detail_book(title):
      book = Book.query.filter_by(title=title).first_or_404()
      result = db.session.query(Book.id, Opinion.body).filter(Opinion.book_id==book.id).filter(and_(Book.title == title)).all()
      return render_template('detail_book.html', book=book, result=result)


      So to sum up, I want to get data from my modal, send it to 1 table in database(Opinion) and display this opinion in my endpoint.
      I have never worked with AJAX, I tried diffrent scripts from web, but in the end I gave up. Can someone help me solve this problem?







      ajax database flask






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 19:46









      FrendomFrendom

      386




      386
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400481%2fupdate-database-using-flask-bootstrap-and-ajax%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400481%2fupdate-database-using-flask-bootstrap-and-ajax%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)