Dec-08-2024, 06:55 AM
(This post was last modified: Dec-08-2024, 07:18 AM by Larz60+.
Edit Reason: fixed code tags
)
Hi, I’m working on a Flask application where employees can generate class codes. I’ve set up a form to handle the submission of class codes (including a description), but I’m unable to generate the class code. Here’s a summary of the issue:
What’s Happening:
- The form is not submitting correctly when I try to generate the class code.
- I don’t see any error messages, but the class code doesn’t get saved in the database.
- I’ve checked the database, and no new records are being added.
- I’m using Flask-WTF for form handling and Flask-SQLAlchemy for database interactions.
Code:
Route:
1. I added debug lines inside the form submission check, but nothing seems to be happening when I submit the form.
2. I’ve ensured that the CSRF token is included in the form (
3. I’ve checked the database for any changes, but no new
Question:
- Why is the form not submitting correctly?
- Why is the class code not being saved to the database?
- What might I be missing or what additional debugging steps can I take to troubleshoot this?
Thanks in advance for your help!
What’s Happening:
- The form is not submitting correctly when I try to generate the class code.
- I don’t see any error messages, but the class code doesn’t get saved in the database.
- I’ve checked the database, and no new records are being added.
- I’m using Flask-WTF for form handling and Flask-SQLAlchemy for database interactions.
Code:
Route:
#python
@main.route('/employee/dashboard', methods=['GET', 'POST'])
@login_required
def employee_dashboard():
if current_user.role != 'employee':
flash('You must be an employee to access this page.', 'danger')
return redirect(url_for('main.dashboard'))
form = EmployeeForm()
class_codes = ClassCode.query.order_by(ClassCode.created_at.desc()).all()
if form.validate_on_submit():
code = form.code.data
description = form.description.data
# Check for duplicates
if ClassCode.query.filter_by(code=code).first():
flash('Class code already exists!', 'danger')
else:
new_code = ClassCode(code=code, description=description)
db.session.add(new_code)
db.session.commit()
flash('Class code generated successfully!', 'success')
return redirect(url_for('main.employee_dashboard'))
return render_template('employee_dashboard.html', form=form, class_codes=class_codes)Class Code Model:python
class ClassCode(db.Model):
id = db.Column(db.Integer, primary_key=True)
code = db.Column(db.String(50), unique=True, nullable=False)
description = db.Column(db.String(100), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return f'<ClassCode {self.code}>'Form:#python
class EmployeeForm(FlaskForm):
code = StringField(
'Class Code',
validators=[DataRequired(), Length(max=20, message="Code must be 20 characters or less.")],
)
description = StringField(
'Description',
validators=[DataRequired(), Length(max=255, message="Description must be 255 characters or less.")],
)
submit = SubmitField('Generate Code')What I’ve Tried:1. I added debug lines inside the form submission check, but nothing seems to be happening when I submit the form.
2. I’ve ensured that the CSRF token is included in the form (
{{ form.hidden_tag() }}).3. I’ve checked the database for any changes, but no new
ClassCode entries are being saved.Question:
- Why is the form not submitting correctly?
- Why is the class code not being saved to the database?
- What might I be missing or what additional debugging steps can I take to troubleshoot this?
Thanks in advance for your help!
