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

          鏡平學校

          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

          Why https connections are so slow when debugging (stepping over) in Java?