Issue
I think I may have been at it too long today and I can't wrap my head around this very well. I am fairly new to python and have been playing with flask for about a week, this is my first 'real' app that I have built so please be gentle.
The app allows an internal customer (datatechs) to enter their ticket number and login into a form. That form is submitted and kept in a sqlite DB for now, with an ID, ticket#, login, create_time and an active flag.
On another page I have what could be called a management list, this renders all the tickets that are currently in the database and marked as active. I have populated this with a jinja2 for loop and have a button that resides inline.
For example:
[ 1 ] [ ticket number ] [ login ] [ button ]
[ 2 ] [ ticket number ] [ login ] [ button ]
The button is my issue. I am very unsure about how I can have the specific button clicked cause an action against that entry in the database. I am looking to flip the status field from an int of 1 to 0 (true and false works too), so these can be essentially marked as complete but still available for analytics.
Thanks for reading, here are some snippets.
views.py
@app.route('/', methods=['GET', 'POST'])
def index():
form = TicketForm()
ticket = Ticket.query.filter_by(status=1)
if request.method == 'GET':
return render_template('index.html', form=form, ticket=ticket)
elif request.method == 'POST':
if form.validate() == False:
flash('All fields are required, please update and try again.')
return render_template('index.html', form=form, ticket=ticket)
else:
remedy = Ticket(request.form['tt'], request.form['login'], create=datetime.datetime.utcnow(), status=1)
db.session.add(remedy)
db.session.commit()
flash('Your ticket has been added to the queue')
return redirect(url_for('index'))
@app.route('/manage', methods=['GET'])
def manage():
if request.method == 'GET':
ticket = Ticket.query.all()
return render_template('manage.html', ticket=ticket)
models.py
class Ticket(db.Model):
id = db.Column(db.Integer, primary_key=True)
tt = db.Column(db.String(10))
user = db.Column(db.String(20))
create = db.Column(DateTime, default=datetime.datetime.utcnow)
status = db.Column(db.Integer)
def __init__(self, tt, user, create, status):
self.tt = tt
self.user = user
self.create = create
self.status = status
manage.html
{% extends "base.html" %}
{% block content %}
<table class="table" width=50%>
<thead>
<tr>
<th>#</th>
<th>Ticket Number</th>
<th>Requester Login</th>
<th>Time Requested (UTC)</th>
<th>Cancel Request</td>
</tr>
</thead>
<tbody>
{% for ticket in ticket %}
<tr>
<td>{{ ticket.id }}</td>
<td>{{ ticket.tt }}</td>
<td>{{ ticket.user }}</td>
<td>{{ ticket.create }}</td>
<td><button type="button" class="btn btn-xs btn-danger">Kill</button></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
Solution
Generally you'd add another view to handle the flip. A note of caution is that some browser helpers preload 'normal' links on webpages to check for viruses and try helpful caching, so avoid using simple links for links that can change data. So we'll use a form instead:
change your <td><button type="button" class="btn btn-xs btn-danger">Kill</button></td>
to:
<td>
<form action="{{ url_for('flip') }}" method="post">
<input type="hidden" name="flip" value="{{ ticket.id }}"/>
<input type="submit" class="btn btn-xs btn-danger" value="Kill"/>
</form>
</td>
Then add a view to do the heavy lifting:
@app.route('/flip', methods=['POST'])
def flip():
ticket = Ticket.query.filter_by(id=request.form["flip"]).first_or_404()
ticket.status = 0
db.session.commit()
return redirect(url_for('index'))
So we're just catching the id
and seeing if it exists in the database, and if so, we'll flip the status
to 0
and then redirect (from flask import redirect
) the user back to the index
view.
Answered By - Doobeh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.