Last active
August 29, 2015 14:09
-
-
Save jmcelroy5/cfe981c80da0d3612d60 to your computer and use it in GitHub Desktop.
Redirects in JS/Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AJAX post to add listing to database | |
$.post("/addlisting", { | |
price: price, | |
comments: comments, | |
latitude: latitude, | |
longitude: longitude | |
}, | |
function(bike_id){ // returning bike_id with server response | |
window.location.href = "/listing/" + bike_id; | |
}); | |
}); | |
In my Flask app: | |
@app.route("/addlisting", methods=['POST']) | |
def add_listing(): | |
new_listing = model.Listing() | |
new_listing.bike_id = flask_session["bike"] # get bike id from flask session | |
new_listing.post_date = datetime.datetime.now() | |
new_listing.post_expiration = datetime.datetime.now() + datetime.timedelta(30) # Post expires 30 days from now | |
new_listing.post_status = "active" | |
new_listing.asking_price = request.form["price"] # FORM | |
new_listing.latitude = request.form["latitude"] # FORM | |
new_listing.longitude = request.form["longitude"] # FORM | |
new_listing.additional_text = request.form["comments"] # FORM | |
# also need to tie the listing to the logged-in user | |
# model.session.add(new_listing) | |
# model.session.commit() | |
flash("Your listing has been created.") | |
return g.bike_id # Bike id from session | |
@app.route("/listing/<int:id>") | |
def listing_success(id): | |
bike = model.session.query(model.Bike).filter_by(id=id).one() | |
return render_template("listing.html", bike=bike) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment