Fetch preventing rendering template in the controller


I’m working on a project for a class and I have starter code that I am editing. they way its supposed to work is that when the user creates a venue it goes to the home page with a message saying venue x has been listed. Once I add the post request in the new_venue.html it does nothing after I click the submit button nothing happens, but I know its doing something because the app.py prints the name that signed up.

Below is Code from the new_venue.html.

I added the script section and the post request

    {% extends 'layouts/main.html' %}
{% block title %}New Venue{% endblock %}
{% block content %}
  <div class="form-wrapper">
    <form id="venueInfo" method="post" class="form">
      <h3 class="form-heading">List a new venue <a href="{{ url_for('index') }}" title="Back to homepage"><i class="fa fa-home pull-right"></i></a></h3>
      <div  class="form-group">
        <label for="name">Name</label>
        {{ form.name(id='name', class_ = 'form-control', autofocus = true) }}
      <div class="form-group">
          <label>City & State</label>
          <div class="form-inline">
            <div id='city' class="form-group">
              {{ form.city(class_ = 'form-control', placeholder='City', autofocus = true) }}
            <div id='state' class="form-group">
              {{ form.state(class_ = 'form-control', placeholder='State', autofocus = true) }}
      <div id='address' class="form-group">
        <label for="address">Address</label>
        {{ form.address(class_ = 'form-control', autofocus = true) }}
      <div id='phone_num' class="form-group">
          <label for="phone">Phone</label>
          {{ form.phone(class_ = 'form-control', placeholder='xxx-xxx-xxxx', autofocus = true) }}
      <div id="genres" class="form-group">
        <label for="genres">Genres</label>
        <small>Ctrl+Click to select multiple</small>
        {{ form.genres(class_ = 'form-control', autofocus = true) }}
      <div id="fb_link" class="form-group">
          <label for="genres">Facebook Link</label>
          {{ form.facebook_link(class_ = 'form-control', placeholder='http://', autofocus = true) }}
      <input type="submit" value="Create Venue" class="btn btn-primary btn-lg btn-block">

    <script type="text/javascript">

          'name': document.getElementById('name').value,
          'city': document.getElementById('city').value,
          'state': document.getElementById('state').value,
          'address': document.getElementById('address').value,
          'phone_num': document.getElementById('phone_num').value,
          'genres': document.getElementById('genres').value,
          'fb_link': document.getElementById('fb_link').value,
        headers: {'Content-type': 'application/json'}



{% endblock %}

below is the code from app.py

    @app.route('/venues/create', methods=['GET'])
def create_venue_form():
  form = VenueForm()
  return render_template('forms/new_venue.html', form=form)

@app.route('/venues/create', methods=['POST'])
def create_venue_submission():
  name = request.get_json()['name']

  flash('Venue ' + request.form['name'] + ' was successfully listed!')

  return render_template('pages/home.html')


flash messages work with redirection, refer to https://flask.palletsprojects.com/en/1.1.x/patterns/flashing/#simple-flashing

so instead rendering the template, return redirection object to the home page:

@app.route('/venues/create', methods=['POST'])
def create_venue_submission():
  # name = request.get_json()['name']
  name = request.values.get('name')


  flash('Venue ' + request.form['name'] + ' was successfully listed!')

  #  return render_template('pages/home.html')
  return redirect(url_for('home'))  # -- HERE --


i think you are doing things the wrong way, you don’t need the javascript to submit the form data via ajax post since ajax is used to update the page without reloading it (btw you didn’t put any logic in .then(function(){}) callback to show up the message), but after submitting the form you want to redirect the user to the home page with a flash message so the ajax approach you are using is the wrong approach, just remove or comment the javascript code block and add the action to the form

<form id="venueInfo" method="post" action="{{ url_for('create_venue_submission') }}" class="form">


and in your function create_venue_submission() you should change

name = request.get_json()['name']


name = request.values.get('name')

# other fields
name = request.values.get('name')
city = request.values.get('city')
state = request.values.get('state')
address = request.values.get('address')
phone_num = request.values.get('phone_num')
genres = request.values.get('genres')
fb_link = request.values.get('fb_link')

see this wiki https://stackoverflow.com/a/16664376/12368419

Answered By – cizario

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published