Create an edit form with previously populated fields, facing difficulties with input[file] and textareaHow do you disable browser Autocomplete on web form field / input tag?How to focus on a form input text field on page load using jQuery?What's the difference between disabled=“disabled” and readonly=“readonly” for HTML form input fields?Flask-WTF form populating integer fields and radio fieldsreset form fields previously populated with $_POST variablesCannot display HTML stringPre-Populate an edit form with WTForms and Flaskflask peewee how to change the field order when edit/creating formsPopulate div input form field value with variable dataHow do I retrieve values from a “User” and pre-populate the form when it comes to html: input[password] and textarea?
Nested-Loop-Join: How many comparisons and how many pages-accesses?
Align by center of symbol
Why hasn't the U.S. government paid war reparations to any country it attacked?
Too many spies!
Can a continent naturally split into two distant parts within a week?
What exactly is the Tension force?
Deep Learning based time series forecasting
Published paper containing well-known results
Why are Japanese translated subtitles non-conversational?
Why does the trade federation become so alarmed upon learning the ambassadors are Jedi Knights?
Nikon FM2 lever cannot advance
Access files in Home directory from live mode
Book or series about stones and a magician named Gwydion
Alternatives to using writing paper for writing practice
Does optical correction here give more aesthetic look to logo?
Possible isometry groups of open manifolds
Why is dry soil hydrophobic? Bad gardener paradox
Cubic programming and beyond?
Meaning of slash chord without anything left of the slash
Why do they not say "The Baby"
How do I define this subset using mathematical notation?
Crab Nebula short story from 1960s or '70s
Does Google Maps take into account hills/inclines for route times?
Absconding a company after 1st day of joining
Create an edit form with previously populated fields, facing difficulties with input[file] and textarea
How do you disable browser Autocomplete on web form field / input tag?How to focus on a form input text field on page load using jQuery?What's the difference between disabled=“disabled” and readonly=“readonly” for HTML form input fields?Flask-WTF form populating integer fields and radio fieldsreset form fields previously populated with $_POST variablesCannot display HTML stringPre-Populate an edit form with WTForms and Flaskflask peewee how to change the field order when edit/creating formsPopulate div input form field value with variable dataHow do I retrieve values from a “User” and pre-populate the form when it comes to html: input[password] and textarea?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am doing a Python Flask project, trying to "edit" a current "object" from postgresql via FlaskForm. So far, I have managed to retrieve the values of the "recipe" object using value for but I need to manually render recipe.description.
However, how do I go about the same using the input[file] to retrieve recipe.picture ?
(https://imgur.com/6zfLjBE)
Here is my form.html
<form method="POST" action=" url_for('recipes.edit_recipe', recipe_id=recipe.id) " enctype="multipart/form-data">
form.csrf_token
form.hidden_tag()
<fieldset class="form-group">
<div class="modal-body">
% for field in form if field.name != "csrf_token" %
% if field.name != "btn" %
<div class="form-group">
field.label(class="form-control-label form-control-lg")
<!-- if fiend.something == title, need to insert title -->
% if field.name == "picture" %
field(class="form-control", value=recipe.picture)
<figure>
<img src="recipe.picture" class="small-preview" alt="preview">
<figcaption>Current photo</figcaption>
</figure>
% endif %
% if field.name == "title" %
field(class="form-control", value=recipe.title)
% endif %
% if field.name == "description" %
<textarea class="form-control" name="description" id="description" cols="30" rows="5"> recipe.description </textarea>
% endif %
<!-- end the if block -->
% for error in field.errors %
error
% endfor %
</div>
% else %
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
form.btn(class="btn btn-info")
</div>
% endif %
% endfor %
</div>
</fieldset>
</form>
Here is my views.py
@recipes_blueprint.route('/<int:recipe_id>', methods=['POST'])
@login_required
def edit_recipe(recipe_id):
form = UpdateRecipeForm()
that_recipe = Recipe.get_or_none(Recipe.id == recipe_id)
recipe_owner = User.get_or_none(User.id == that_recipe.user_id)
if form.validate_on_submit():
if recipe_owner != current_user:
flash("Only owner of the recipe can amend the details", "danger")
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
else:
if "picture" not in request.files:
flash("No picture key in request.files", 'danger')
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
file = request.files['picture']
if file.filename == '':
flash("Please select a file", 'danger')
return redirect(url_for('users.update_user'))
if file and allowed_file(file.filename):
file.filename = secure_filename(file.filename)
output = upload_file_to_s3(file, app.config['S3_BUCKET'])
updated_recipe = Recipe.update(
title=form.data['title'],
description=form.data['description'],
picture=output
).where(Recipe.user_id == recipe_owner.id)
updated_recipe.execute()
flash("Recipe updated successfully", "success")
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
return render_template('read_that_recipe.html', recipe=that_recipe, form=form)
html python-3.x flask jinja2 peewee
add a comment |
I am doing a Python Flask project, trying to "edit" a current "object" from postgresql via FlaskForm. So far, I have managed to retrieve the values of the "recipe" object using value for but I need to manually render recipe.description.
However, how do I go about the same using the input[file] to retrieve recipe.picture ?
(https://imgur.com/6zfLjBE)
Here is my form.html
<form method="POST" action=" url_for('recipes.edit_recipe', recipe_id=recipe.id) " enctype="multipart/form-data">
form.csrf_token
form.hidden_tag()
<fieldset class="form-group">
<div class="modal-body">
% for field in form if field.name != "csrf_token" %
% if field.name != "btn" %
<div class="form-group">
field.label(class="form-control-label form-control-lg")
<!-- if fiend.something == title, need to insert title -->
% if field.name == "picture" %
field(class="form-control", value=recipe.picture)
<figure>
<img src="recipe.picture" class="small-preview" alt="preview">
<figcaption>Current photo</figcaption>
</figure>
% endif %
% if field.name == "title" %
field(class="form-control", value=recipe.title)
% endif %
% if field.name == "description" %
<textarea class="form-control" name="description" id="description" cols="30" rows="5"> recipe.description </textarea>
% endif %
<!-- end the if block -->
% for error in field.errors %
error
% endfor %
</div>
% else %
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
form.btn(class="btn btn-info")
</div>
% endif %
% endfor %
</div>
</fieldset>
</form>
Here is my views.py
@recipes_blueprint.route('/<int:recipe_id>', methods=['POST'])
@login_required
def edit_recipe(recipe_id):
form = UpdateRecipeForm()
that_recipe = Recipe.get_or_none(Recipe.id == recipe_id)
recipe_owner = User.get_or_none(User.id == that_recipe.user_id)
if form.validate_on_submit():
if recipe_owner != current_user:
flash("Only owner of the recipe can amend the details", "danger")
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
else:
if "picture" not in request.files:
flash("No picture key in request.files", 'danger')
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
file = request.files['picture']
if file.filename == '':
flash("Please select a file", 'danger')
return redirect(url_for('users.update_user'))
if file and allowed_file(file.filename):
file.filename = secure_filename(file.filename)
output = upload_file_to_s3(file, app.config['S3_BUCKET'])
updated_recipe = Recipe.update(
title=form.data['title'],
description=form.data['description'],
picture=output
).where(Recipe.user_id == recipe_owner.id)
updated_recipe.execute()
flash("Recipe updated successfully", "success")
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
return render_template('read_that_recipe.html', recipe=that_recipe, form=form)
html python-3.x flask jinja2 peewee
add a comment |
I am doing a Python Flask project, trying to "edit" a current "object" from postgresql via FlaskForm. So far, I have managed to retrieve the values of the "recipe" object using value for but I need to manually render recipe.description.
However, how do I go about the same using the input[file] to retrieve recipe.picture ?
(https://imgur.com/6zfLjBE)
Here is my form.html
<form method="POST" action=" url_for('recipes.edit_recipe', recipe_id=recipe.id) " enctype="multipart/form-data">
form.csrf_token
form.hidden_tag()
<fieldset class="form-group">
<div class="modal-body">
% for field in form if field.name != "csrf_token" %
% if field.name != "btn" %
<div class="form-group">
field.label(class="form-control-label form-control-lg")
<!-- if fiend.something == title, need to insert title -->
% if field.name == "picture" %
field(class="form-control", value=recipe.picture)
<figure>
<img src="recipe.picture" class="small-preview" alt="preview">
<figcaption>Current photo</figcaption>
</figure>
% endif %
% if field.name == "title" %
field(class="form-control", value=recipe.title)
% endif %
% if field.name == "description" %
<textarea class="form-control" name="description" id="description" cols="30" rows="5"> recipe.description </textarea>
% endif %
<!-- end the if block -->
% for error in field.errors %
error
% endfor %
</div>
% else %
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
form.btn(class="btn btn-info")
</div>
% endif %
% endfor %
</div>
</fieldset>
</form>
Here is my views.py
@recipes_blueprint.route('/<int:recipe_id>', methods=['POST'])
@login_required
def edit_recipe(recipe_id):
form = UpdateRecipeForm()
that_recipe = Recipe.get_or_none(Recipe.id == recipe_id)
recipe_owner = User.get_or_none(User.id == that_recipe.user_id)
if form.validate_on_submit():
if recipe_owner != current_user:
flash("Only owner of the recipe can amend the details", "danger")
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
else:
if "picture" not in request.files:
flash("No picture key in request.files", 'danger')
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
file = request.files['picture']
if file.filename == '':
flash("Please select a file", 'danger')
return redirect(url_for('users.update_user'))
if file and allowed_file(file.filename):
file.filename = secure_filename(file.filename)
output = upload_file_to_s3(file, app.config['S3_BUCKET'])
updated_recipe = Recipe.update(
title=form.data['title'],
description=form.data['description'],
picture=output
).where(Recipe.user_id == recipe_owner.id)
updated_recipe.execute()
flash("Recipe updated successfully", "success")
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
return render_template('read_that_recipe.html', recipe=that_recipe, form=form)
html python-3.x flask jinja2 peewee
I am doing a Python Flask project, trying to "edit" a current "object" from postgresql via FlaskForm. So far, I have managed to retrieve the values of the "recipe" object using value for but I need to manually render recipe.description.
However, how do I go about the same using the input[file] to retrieve recipe.picture ?
(https://imgur.com/6zfLjBE)
Here is my form.html
<form method="POST" action=" url_for('recipes.edit_recipe', recipe_id=recipe.id) " enctype="multipart/form-data">
form.csrf_token
form.hidden_tag()
<fieldset class="form-group">
<div class="modal-body">
% for field in form if field.name != "csrf_token" %
% if field.name != "btn" %
<div class="form-group">
field.label(class="form-control-label form-control-lg")
<!-- if fiend.something == title, need to insert title -->
% if field.name == "picture" %
field(class="form-control", value=recipe.picture)
<figure>
<img src="recipe.picture" class="small-preview" alt="preview">
<figcaption>Current photo</figcaption>
</figure>
% endif %
% if field.name == "title" %
field(class="form-control", value=recipe.title)
% endif %
% if field.name == "description" %
<textarea class="form-control" name="description" id="description" cols="30" rows="5"> recipe.description </textarea>
% endif %
<!-- end the if block -->
% for error in field.errors %
error
% endfor %
</div>
% else %
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
form.btn(class="btn btn-info")
</div>
% endif %
% endfor %
</div>
</fieldset>
</form>
Here is my views.py
@recipes_blueprint.route('/<int:recipe_id>', methods=['POST'])
@login_required
def edit_recipe(recipe_id):
form = UpdateRecipeForm()
that_recipe = Recipe.get_or_none(Recipe.id == recipe_id)
recipe_owner = User.get_or_none(User.id == that_recipe.user_id)
if form.validate_on_submit():
if recipe_owner != current_user:
flash("Only owner of the recipe can amend the details", "danger")
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
else:
if "picture" not in request.files:
flash("No picture key in request.files", 'danger')
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
file = request.files['picture']
if file.filename == '':
flash("Please select a file", 'danger')
return redirect(url_for('users.update_user'))
if file and allowed_file(file.filename):
file.filename = secure_filename(file.filename)
output = upload_file_to_s3(file, app.config['S3_BUCKET'])
updated_recipe = Recipe.update(
title=form.data['title'],
description=form.data['description'],
picture=output
).where(Recipe.user_id == recipe_owner.id)
updated_recipe.execute()
flash("Recipe updated successfully", "success")
return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
return render_template('read_that_recipe.html', recipe=that_recipe, form=form)
html python-3.x flask jinja2 peewee
html python-3.x flask jinja2 peewee
asked Mar 26 at 6:28
Chai Chong TehChai Chong Teh
385 bronze badges
385 bronze badges
add a comment |
add a comment |
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
);
);
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%2f55351009%2fcreate-an-edit-form-with-previously-populated-fields-facing-difficulties-with-i%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
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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.
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%2f55351009%2fcreate-an-edit-form-with-previously-populated-fields-facing-difficulties-with-i%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