我正在尝试获取使用wtforms创建的表单,而在我的终端中,我看到的只是这样:
"POST /venues/create HTTP/1.1" 302
一个小问题是我希望我能得到一个更具描述性的错误。有可能吗?
但是我的主要问题是,当我提交表单时,它会重定向到索引并闪烁错误消息。闪烁错误时,将显示正确的名称。我正在使用python3,flask,wtforms和flask-sqlalchemy。我不知道我在做什么错。这是相关代码:
app.py:
class Venue(db.Model):
__tablename__ = 'venue'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
city = db.Column(db.String(120))
state = db.Column(db.String(120))
address = db.Column(db.String(120))
phone = db.Column(db.String(120))
image_link = db.Column(db.String(500))
facebook_link = db.Column(db.String(120))
seeking_talent = db.Column(db.Boolean, nullable=False, default=False)
seeking_description = db.Column(db.String())
genres = db.Column(db.String())
website = db.Column(db.String())
past_shows = db.relationship('Show', backref='shows', lazy=True)
我已经尝试过了:
@app.route('/venues/create', methods=['POST'])
def create_venue_submission():
error = False
form = VenueForm(request.form)
name = request.form.get('name')
city = request.form.get('city')
state = request.form.get('state')
address = request.form.get('address')
phone = request.form.get('phone')
image = request.form.get('image_link')
facebook = request.form.get('facebook_link')
seeking = request.form.get('seeking_talent')
seekingDesc = request.form.get('seeking_description')
genres = request.form.get('genres')
website = request.form.get('website')
try:
venue = Venue(name=name,
city=city,
state=state,
address=address,
phone=phone,
image_link=image,
facebook_link=facebook,
seeking_talent=seeking,
seeking_description=seekingDesc,
genres=genres,
website=website)
db.session.add(venue)
db.session.commit()
except:
error = True
db.session.rollback()
finally:
db.session.close()
if error:
flash('An error occured. Venue ' + name + ' could not be listed.')
return redirect(url_for('index'))
else:
flash('Venue ' + request.form['name'] + ' was successfully listed!')
return redirect(url_for('index'))
这:
@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():
error = False
#form = VenueForm(request.form)
name = request.form['name']
city = request.form['city']
state = request.form['state']
address = request.form['address']
phone = request.form['phone']
image = request.form['image_link']
facebook = request.form['facebook_link']
seeking = request.form['seeking_talent']
seekingDesc = request.form['seeking_description']
genres = request.form['genres']
website = request.form['website']
try:
venue = Venue(name=name,
city=city,
state=state,
address=address,
phone=phone,
image_link=image,
facebook_link=facebook,
seeking_talent=seeking,
seeking_description=seekingDesc,
genres=genres,
website=website)
db.session.add(venue)
db.session.commit()
except:
error = True
db.session.rollback()
finally:
db.session.close()
if error:
flash('An error occured. Venue ' + name + ' could not be listed.')
return redirect(url_for('index'))
else:
flash('Venue ' + request.form['name'] + ' was successfully listed!')
return redirect(url_for('index'))
forms.py:
class VenueForm(Form):
name = StringField(
'name', validators=[DataRequired()]
)
city = StringField(
'city', validators=[DataRequired()]
)
state = SelectField(
'state', validators=[DataRequired()],
choices=[
('AL', 'AL'),
('WY', 'WY'),
]
)
address = StringField(
'address', validators=[DataRequired()]
)
phone = StringField(
'phone'
)
image_link = StringField(
'image_link', validators=[URL()]
)
genres = SelectMultipleField(
# TODO implement enum restriction
'genres', validators=[DataRequired()],
choices=[
('Alternative', 'Alternative'),
('Blues', 'Blues'),
('Classical', 'Classical'),
('Country', 'Country'),
('Electronic', 'Electronic'),
('Folk', 'Folk'),
('Funk', 'Funk'),
('Hip-Hop', 'Hip-Hop'),
('Heavy Metal', 'Heavy Metal'),
('Instrumental', 'Instrumental'),
('Jazz', 'Jazz'),
('Musical Theatre', 'Musical Theatre'),
('Pop', 'Pop'),
('Punk', 'Punk'),
('R&B', 'R&B'),
('Reggae', 'Reggae'),
('Rock n Roll', 'Rock n Roll'),
('Soul', 'Soul'),
('Other', 'Other'),
]
)
seeking_talent = SelectField(
'seeking_talent', validators=[DataRequired()],
choices=[
(True, 'Yes'),
(False, 'No')
]
)
seeking_description = StringField(
'seeking_description'
)
facebook_link = StringField(
# TODO implement enum restriction
'facebook_link', validators=[URL()]
)
website = StringField(
# TODO implement enum restriction
'website', validators=[URL()]
)
new_venue.html
<div class="form-wrapper">
<form action="/venues/create" 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(class_ = 'form-control', id=name, autofocus = true) }}
</div>
<div class="form-group">
<label>City & State</label>
<div class="form-inline">
<div class="form-group">
{{ form.city(class_ = 'form-control', placeholder='City', id=city, autofocus = true) }}
</div>
<div class="form-group">
{{ form.state(class_ = 'form-control', placeholder='State', id=form.state.id, autofocus = true) }}
</div>
</div>
</div>
<div class="form-group">
<label for="address">Address</label>
{{ form.address(class_ = 'form-control', id=address, autofocus = true) }}
</div>
<div class="form-group">
<label for="phone">Phone</label>
{{ form.phone(class_ = 'form-control', placeholder='xxx-xxx-xxxx', id=phone, autofocus = true) }}
</div>
<div class="form-group">
<label for="genres">Genres</label>
<small>Ctrl+Click to select multiple</small>
{{ form.genres(class_ = 'form-control', placeholder='Genres, separated by commas', id=form.genres.id, autofocus = true) }}
</div>
<div class="form-group">
<label for="seeking_talent">Is Venue currently seeking talent?</label>
{{ form.seeking_talent(class_ = 'form-control', id=form.seeking_talent.id, autofocus = true) }}
</div>
<div class="form-group">
<label for="seeking_description">What kind of music are you looking for?</label>
{{ form.seeking_description(class_ = 'form-control', id=seeking_description, autofocus = true) }}
</div>
<div class="form-group">
<label for="facebook_link">Facebook Link</label>
{{ form.facebook_link(class_ = 'form-control', placeholder='http://', id=facebook_link, autofocus = true) }}
</div>
<div class="form-group">
<label for="image_link">Image of Venue</label>
{{ form.image_link(class_ = 'form-control', id=image_link, autofocus = true) }}
</div>
<div class="form-group">
<label for="website">Website</label>
{{ form.website(class_ = 'form-control', id=website, placeholder='http://', autofocus = true) }}
</div>
<input type="submit" value="Create Venue" class="btn btn-primary btn-lg btn-block">
</form>
</div>
我在做什么错了?