So I'm having a bit of an issue here. I'm pretty new to Flask and web dev in general. I'm trying to add a new item in a database with SQLAlchemy and for some reason it's not working. This is my first practice project and it's extremely basic. You're likely going to find many issues; things that break best practice rules. I understand that, but please be indulgent. I'm just trying to get the very basics to work, then I'll improve the code.
There is the database model:
class TaskType(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False, unique=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
name = db.Column(db.Text(255))
color_code = db.Column(db.Text, default='NULL')
creation_timestamp = db.Column(db.DateTime, default=datetime.utcnow())
last_update_timestamp = db.Column(db.DateTime, default='NULL')
def __repr__(self):
return '<Task type %r>' % self.id
Here is the code for the app.route code:
@app.route('/options/tasks-types', methods=['POST', 'GET'])
def options_tasks_types():
global active_user_id
if active_user_id != 0:
if request.method == 'POST':
task_type_name = request.form['name']
task_type_color = request.form['color_code']
timestamp = datetime.now().replace(microsecond=0)
new_task_type = TaskType(user_id=active_user_id,
name=task_type_name,
color_code=task_type_color,
creation_timestamp=timestamp,
last_update_timestamp='NULL')
try:
db.session.add(new_task_type)
db.session.commit()
return redirect('/options/task-types')
except:
return "The task type could not be created."
else:
task_types = TaskType.query.filter(User.id == active_user_id).order_by(TaskType.name.desc()).all()
return render_template('options_tasks_types.html', task_types=task_types)
else:
return "You were disconnected."
Here is the HTML form:
{% extends 'base.html' %}
{% block head %}
<title>Task Manager</title>
{% endblock %}
{% block body %}
<div class="content" style="padding: 50px">
<h1>Task types</h1>
<table id="tasks_types">
<tr>
<th>Name</th>
<th>Color</th>
</tr>
{% for task_type in task_types %}
<tr>
<td>{{ task_type.name }}</td>
<td>
<div class="color-box" style="background-color: {{ task_type.color_code }} ;"></div>
</div>
</td>
<td>
<a href="">Edit</a>
<br>
<a href="">Delete</a>
</td>
</tr>
{% endfor %}
<tr>
<form action="{{ url_for('options_tasks_types') }}" method="POST">
<td>
<input type="text" name="name" id="name" placeholder="Task type name">
</td>
<td>
<input type="text" name="color_code" id="color_code" placeholder="Color code">
</td>
<td>
<input type="submit" value="Add new task type">
</td>
</form>
</tr>
</table>
</div>
{% endblock %}
For some reason I keep getting the Except message "The task could not be created". Unfortunately I can't really know why the exception is even happening. Is there a way to get a more detailed error message?
I've tried removing the try and except statements to just get the Flask error message, but it's not exactly detailed...
All I get is:
Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
I have a feeling that the issue is probably coming from my model and that an argument for one of the columns is preventing the TaskType item to be added to the database, but I can't figure out what it is.
Could someone help?
except:, it catches everything, includingSystemExitand interrupts. It is sometimes used to handle cleanup (rollback), but in that case you would always reraisethe original exception instead of supressing it. Catching exceptions when you don't actually handle the error is usually not a good idea, and hinders debugging as seen here.