'I want the dropdown choice to be the one registered in my database with Flask
I have some code, but it gives me an error. It's done with Flask, and here is the error: https://hastebin.com/emifitopof.sql
File forms.py
class PostForm(FlaskForm):
titre = TextAreaField('Ecrivez votre titre', validators=[
DataRequired(), Length(min=1, max=40)])
post = TextAreaField('Ecrivez quelque chose', validators=[
DataRequired(), Length(min=1, max=140)])
searched_tag = TextAreaField('Ecrivez vos tags', validators=[
DataRequired(), Length(min=1, max=140)])
matiere_dropdown_list = [('matiere', 'Matière :'), ('FR','Français'), ('HG','Histoire Géographie'), ('AN','Anglais'), ('ES','Espagnol'), ('IT','Italien'), ('AL','Allemand'), ('SES','SES'), ('Math','Mathématiques'), ('SVT','SVT'), ('EMC','EMC'), ('SNT','SNT'), ('Philo','Philosophie'), ('SI','SI'), ('NSI','NSI')]
matiere = SelectField('Matieres', choices=matiere_dropdown_list, default=1)
niveau_dropdown_list = [('niveau', 'Niveau :'), ('2GT','2nd GT'), ('2PRO','2ndPro'), ('1G','1ereG'), ('1STI2D','1ereSTI2D'), ('1STL','1ereSTL'), ('1STD2A','1ereSTD2A'), ('1ST2S','1ereST2S'), ('1STMG','1ereSTMG'), ('1ST2MD','1ereS2TMD'), ('1STAV','1ereSTAV'), ('1STHR','1ereSTHR'), ('TSTI2D','TermSTI2D'), ('TSTL','TermSTL'), ('TSTD2A','TermSTD2A'), ('TST2S','TermST2S'), ('TSTMG','TermSTMG'), ('TS2TMD','TermS2TMD'), ('TSTAV','TermSTAV'),('TSTHR','TermSTHR')]
niveau = SelectField('Niveaux', choices=niveau_dropdown_list, default=1)
submit = SubmitField('Envoyer')
File models.py
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
titre = db.Column(db.String(40))
body = db.Column(db.String(140))
searched_tag = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
matiere_dropdown_list = db.Column(db.String(40))
niveau_dropdown_list = db.Column(db.String(20))
def __repr__(self):
return '<Post {}>'.format(self.body)
File routes.py
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
@login_required
def index():
form = PostForm()
if form.validate_on_submit():
form.matiere_dropdown_list.append_entry()
form.niveau_dropdown_list.append_entry()
post = Post(titre=form.titre.data, body=form.post.data, author=current_user, searched_tag=form.searched_tag.data, matiere_dropdown_list=form.matiere_dropdown_list, niveau_dropdown_list=form.niveau_dropdown_list)
db.session.add(post)
db.session.commit()
flash('Votre post est désormais visible aux yeux de tous !')
return redirect(url_for('index'))
page = request.args.get('page', 1, type=int)
posts = Post.query.order_by(Post.timestamp.desc()).paginate(
page, app.config['POSTS_PER_PAGE'], False)
next_url = url_for('index', page=posts.next_num) \
if posts.has_next else None
prev_url = url_for('index', page=posts.prev_num) \
if posts.has_prev else None
return render_template('index.html', title='Home', form=form,
posts=posts.items, next_url=next_url,
prev_url=prev_url)
I don't know how to adapt my code to get the choice (the selected option).
How can I do it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|