Created
October 22, 2013 18:32
-
-
Save johnschimmel/7105640 to your computer and use it in GitHub Desktop.
Flask route for updating a record
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
@app.route("/ideas/edit/<idea_id>", methods=['GET','POST']) | |
def idea_edit(idea_id): | |
if request.method == 'POST': | |
try: | |
idea = models.Idea.objects.get(id=request.form.get('idea_id')) | |
except: | |
abort(404) | |
# populate the IdeaForm with incoming form data | |
ideaForm = models.IdeaForm(request.form) | |
if ideaForm.validate(): | |
updateData = { | |
'set__title' : request.form.get('title'), | |
'set__creator' : request.form.get('creator'), | |
'set__idea' : request.form.get('idea'), | |
'set__categories' : request.form.getlist('categories') | |
} | |
idea.update(**updateData) # update the idea | |
# redirect to the GET method of the current page | |
return redirect('/ideas/edit/%s' % idea.id ) | |
else: | |
# get the idea convert it to the model form, this prepopulates the form | |
try: | |
idea = models.Idea.objects.get(id=idea_id) | |
ideaForm = models.IdeaForm(obj=idea) | |
except: | |
abort(404) | |
templateData = { | |
'idea_id' : idea.id, | |
'form' : ideaForm, | |
'categories' : categories | |
} | |
return render_template('idea_edit.html', **templateData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment