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;








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)
enter image description here



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)









share|improve this question




























    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)
    enter image description here



    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)









    share|improve this question
























      0












      0








      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)
      enter image description here



      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)









      share|improve this question














      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)
      enter image description here



      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 6:28









      Chai Chong TehChai Chong Teh

      385 bronze badges




      385 bronze badges






















          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
          );



          );













          draft saved

          draft discarded


















          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.



















          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.




          draft saved


          draft discarded














          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





















































          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

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?