How can I save a previous URL in Flask?
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
add a comment |
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
add a comment |
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
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
python flask jinja2 flask-sqlalchemy
edited Nov 23 '18 at 19:10
user6910411
32.8k87095
32.8k87095
asked Nov 23 '18 at 18:04
user10694432
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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">
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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">
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
add a comment |
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">
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
add a comment |
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">
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">
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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