Skip to content
Snippets Groups Projects
Commit 414d0cbe authored by Felix Chadwick-Smith's avatar Felix Chadwick-Smith
Browse files

Merge branch 'master' into 'main'

Master

See merge request !21
parents 0f91fbca 491bd3b4
No related branches found
No related tags found
1 merge request!21Master
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import DataRequired
class ProjectForm(FlaskForm):
title = StringField('Title', validators=[DataRequired()])
description = TextAreaField('Description', validators=[DataRequired()])
submit = SubmitField('Add Project')
......@@ -2,8 +2,10 @@ import os
import secrets
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, abort, current_app
from flask_sqlalchemy import SQLAlchemy
from models import db, Project
from flask_wtf import FlaskForm
from forms import ProjectForm
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import DataRequired
app = Flask(__name__, static_folder='static')
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
......@@ -11,12 +13,18 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.abspath(os.path.j
app.config['SECRET_KEY'] = secrets.token_hex(16)
db = SQLAlchemy(app)
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text, nullable=False)
class ProjectForm(FlaskForm):
title = StringField('Title', validators=[DataRequired()])
description = TextAreaField('Description', validators=[DataRequired()])
submit = SubmitField('Add Project')
@app.route('/')
def home():
try:
projects = Project.query.all()
......@@ -47,23 +55,22 @@ def portfolio():
def contact():
return render_template('contact.html')
# Updated route for adding a project without Flask-WTF form
# Updated route for adding a project using Flask-WTF form
@app.route('/add_project', methods=['GET', 'POST'])
def add_project():
if request.method == 'POST':
# Retrieve form data directly from request
title = request.form.get('title')
description = request.form.get('description')
form = ProjectForm()
# Print or log the form data to check if it's received
print(f"Received form data - Title: {title}, Description: {description}")
if request.method == 'POST' and form.validate_on_submit():
title = form.title.data
description = form.description.data
new_project = Project(title=title, description=description)
db.session.add(new_project)
db.session.commit()
return redirect(url_for('home'))
return render_template('add_project.html')
return render_template('add_project.html', form=form) # Pass the form to the template
# Updated route for serving the 'my-cv.docx' file
@app.route('/download_cv')
......
......@@ -19,3 +19,6 @@ waitress==2.1.2
Werkzeug==3.0.1
zope.event==5.0
zope.interface==6.1
Flask-WTF==1.2.1
wtforms==3.1.2
......@@ -11,6 +11,7 @@
<body>
<h2>Add Project</h2>
<form method="POST" action="{{ url_for('add_project') }}">
{% if form %}
{{ form.csrf_token }}
<div class="form-group">
{{ form.title.label }}
......@@ -23,6 +24,7 @@
<div class="form-group">
{{ form.submit(class="btn btn-primary") }}
</div>
{% endif %}
</form>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment