How can I save a previous URL in Flask?












0














I am creating a book store and one of the functionalities is that once I am on a page, I would like to filter it more and only show 10 or 20 results per page.



<form action="{{ url_for('show_10') }}">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Show Results
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu2">
<button class="dropdown-item" value = "{{ request.path }}" type="button">Show 10 </button>
</div>
</div>
</form>


So what I want to do is if the current path is a certain route i.e. "/sort_by_rating", then it should go to a function that will only shows results as 10 per page for that search.



@app.route('/show_10', methods = ['GET', 'POST'])
def show_10():
page = request.args.get('page', 1, type=int)
print(request.data['name'])
if request.method == 'POST':
if "/sort_by_bestseller" in request.form:
books = Book.query.filter(Book.best_seller == True).paginate(page=page, per_page=10)
if "/sort_by_rating" in request.form:
books = Book.query.order_by(Book.rating).paginate(page=page, per_page=10)
if "/sort_by_date" in request.form:
books = Book.query.order_by(Book.date).paginate(page=page, per_page=10)
return render_template('search_by_author.html', books=books)


I am very new to web development. I am not sure if my logic is correct. However when I click on the button to show 10 results, it just stays on the current page and no changes happen. Any help is appreciated.










share|improve this question





























    0














    I am creating a book store and one of the functionalities is that once I am on a page, I would like to filter it more and only show 10 or 20 results per page.



    <form action="{{ url_for('show_10') }}">
    <div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Show Results
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
    <button class="dropdown-item" value = "{{ request.path }}" type="button">Show 10 </button>
    </div>
    </div>
    </form>


    So what I want to do is if the current path is a certain route i.e. "/sort_by_rating", then it should go to a function that will only shows results as 10 per page for that search.



    @app.route('/show_10', methods = ['GET', 'POST'])
    def show_10():
    page = request.args.get('page', 1, type=int)
    print(request.data['name'])
    if request.method == 'POST':
    if "/sort_by_bestseller" in request.form:
    books = Book.query.filter(Book.best_seller == True).paginate(page=page, per_page=10)
    if "/sort_by_rating" in request.form:
    books = Book.query.order_by(Book.rating).paginate(page=page, per_page=10)
    if "/sort_by_date" in request.form:
    books = Book.query.order_by(Book.date).paginate(page=page, per_page=10)
    return render_template('search_by_author.html', books=books)


    I am very new to web development. I am not sure if my logic is correct. However when I click on the button to show 10 results, it just stays on the current page and no changes happen. Any help is appreciated.










    share|improve this question



























      0












      0








      0







      I am creating a book store and one of the functionalities is that once I am on a page, I would like to filter it more and only show 10 or 20 results per page.



      <form action="{{ url_for('show_10') }}">
      <div class="dropdown">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Show Results
      </button>
      <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
      <button class="dropdown-item" value = "{{ request.path }}" type="button">Show 10 </button>
      </div>
      </div>
      </form>


      So what I want to do is if the current path is a certain route i.e. "/sort_by_rating", then it should go to a function that will only shows results as 10 per page for that search.



      @app.route('/show_10', methods = ['GET', 'POST'])
      def show_10():
      page = request.args.get('page', 1, type=int)
      print(request.data['name'])
      if request.method == 'POST':
      if "/sort_by_bestseller" in request.form:
      books = Book.query.filter(Book.best_seller == True).paginate(page=page, per_page=10)
      if "/sort_by_rating" in request.form:
      books = Book.query.order_by(Book.rating).paginate(page=page, per_page=10)
      if "/sort_by_date" in request.form:
      books = Book.query.order_by(Book.date).paginate(page=page, per_page=10)
      return render_template('search_by_author.html', books=books)


      I am very new to web development. I am not sure if my logic is correct. However when I click on the button to show 10 results, it just stays on the current page and no changes happen. Any help is appreciated.










      share|improve this question















      I am creating a book store and one of the functionalities is that once I am on a page, I would like to filter it more and only show 10 or 20 results per page.



      <form action="{{ url_for('show_10') }}">
      <div class="dropdown">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Show Results
      </button>
      <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
      <button class="dropdown-item" value = "{{ request.path }}" type="button">Show 10 </button>
      </div>
      </div>
      </form>


      So what I want to do is if the current path is a certain route i.e. "/sort_by_rating", then it should go to a function that will only shows results as 10 per page for that search.



      @app.route('/show_10', methods = ['GET', 'POST'])
      def show_10():
      page = request.args.get('page', 1, type=int)
      print(request.data['name'])
      if request.method == 'POST':
      if "/sort_by_bestseller" in request.form:
      books = Book.query.filter(Book.best_seller == True).paginate(page=page, per_page=10)
      if "/sort_by_rating" in request.form:
      books = Book.query.order_by(Book.rating).paginate(page=page, per_page=10)
      if "/sort_by_date" in request.form:
      books = Book.query.order_by(Book.date).paginate(page=page, per_page=10)
      return render_template('search_by_author.html', books=books)


      I am very new to web development. I am not sure if my logic is correct. However when I click on the button to show 10 results, it just stays on the current page and no changes happen. Any help is appreciated.







      python flask jinja2 flask-sqlalchemy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 19:10









      user6910411

      32.8k87095




      32.8k87095










      asked Nov 23 '18 at 18:04









      user10694432

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your function only shows the result if the request is a post. But you never told your form to actually send a post. You need to do that with the method attribute:



          <form action="{{ url_for('show_10') }}" method="POST">





          share|improve this answer





















          • Yes, I fixed that issue however I am still getting the same results. When I click on the button, it does not move to another page.
            – user10694432
            Nov 24 '18 at 1:08











          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%2f53451220%2fhow-can-i-save-a-previous-url-in-flask%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your function only shows the result if the request is a post. But you never told your form to actually send a post. You need to do that with the method attribute:



          <form action="{{ url_for('show_10') }}" method="POST">





          share|improve this answer





















          • Yes, I fixed that issue however I am still getting the same results. When I click on the button, it does not move to another page.
            – user10694432
            Nov 24 '18 at 1:08
















          0














          Your function only shows the result if the request is a post. But you never told your form to actually send a post. You need to do that with the method attribute:



          <form action="{{ url_for('show_10') }}" method="POST">





          share|improve this answer





















          • Yes, I fixed that issue however I am still getting the same results. When I click on the button, it does not move to another page.
            – user10694432
            Nov 24 '18 at 1:08














          0












          0








          0






          Your function only shows the result if the request is a post. But you never told your form to actually send a post. You need to do that with the method attribute:



          <form action="{{ url_for('show_10') }}" method="POST">





          share|improve this answer












          Your function only shows the result if the request is a post. But you never told your form to actually send a post. You need to do that with the method attribute:



          <form action="{{ url_for('show_10') }}" method="POST">






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 18:12









          Daniel Roseman

          444k41576632




          444k41576632












          • Yes, I fixed that issue however I am still getting the same results. When I click on the button, it does not move to another page.
            – user10694432
            Nov 24 '18 at 1:08


















          • Yes, I fixed that issue however I am still getting the same results. When I click on the button, it does not move to another page.
            – user10694432
            Nov 24 '18 at 1:08
















          Yes, I fixed that issue however I am still getting the same results. When I click on the button, it does not move to another page.
          – user10694432
          Nov 24 '18 at 1:08




          Yes, I fixed that issue however I am still getting the same results. When I click on the button, it does not move to another page.
          – user10694432
          Nov 24 '18 at 1:08


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53451220%2fhow-can-i-save-a-previous-url-in-flask%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

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks