Faryus
Faryus

Reputation: 161

Flask - pass variable from URL to function

I'm new to Flask so if I say anything stupid, please forgive and forget.

I am trying to make a simple ticketing app and I've gotten to the point where I need to make an "Assign" button so that a certain ticket can be assigned to a certain user.

My bits of code currently look like this:

The template code:

{% for ticket in tickets %}
<tr {% if ticket.ticket_status == 0 %} class="warning" {% elif ticket.ticket_status == 1 %} class="info" {% endif %}>
    <td>{{ ticket.ticket_id }}</td>
    <td>{{ ticket.loc_name }}</td>
    <td>{{ ticket.loc_ip }}</td>
    <td>{{ ticket.ticket_title }}</td>
    <td><a class="btn btn-default btn-xs" href="#">Read</a></td>
    <td>{{ ticket.ticket_status }}</td>
    <td>
        {% if ticket.ticket_status == 0 %}
        <a class="btn btn-info btn-xs" href="{{ url_for('assign_to', ticket_id = ticket.ticket_id) }}">Assign</a>
        {% else %}
        {{ ticket.ticket_owner }}
        {% endif %}
    </td>
    <td>{{ ticket.ticket_open }}</td>
    <td>{{ ticket.ticket_assign }}</td>
    <td>
        {% if not ticket.ticket_close and ticket.ticket_status == 0 %}
        <a class="btn btn-danger btn-xs" href="">Close</a>
        {% elif not ticket.ticket_close and ticket.ticket_status == 1 %}
        <a class="btn btn-success btn-xs" href="">Close</a>
        {% elif ticket.ticket_status == 2 %}
        {{ ticket.ticket_close }}
        {% endif %}
    </td>
    <td>{{ ticket.time_spent }}</td>
</tr>
{% endfor %}

The flask bit:

@planck.route("/admin")
def admin_area():
    cur = g.db.execute("SELECT * FROM tickets ORDER BY ticket_id DESC")
    tickets = [dict(ticket_id=row[0], loc_name=row[1], loc_ip=row[2], ticket_title=row[3], ticket=row[4], ticket_status=row[5], ticket_owner=row[6], ticket_open=row[7], ticket_assign=row[8], ticket_close=row[9], time_spent=row[10]) for row in cur.fetchall()]
    return render_template("admin.html", tickets=tickets)

""" This is what I'm trying to assign with """
@planck.route("/admin/assign_ticket")
def assign_to():
    user = "alex"
    g.db.execute("INSERT INTO tickets (ticket_owner, ticket_assign) VALUES (?, DATETIME('NOW')) WHERE ticket_id='?'", [user, ?])
    g.db.commit()
    return redirect(url_for("admin_area"))

Everything essentially works, except that I don't know how to get the ticket_id from the URL of the anchor. The anchor looks like this: http://planck:5000/admin/assign_ticket?ticket_id=12

Can anyone share some knowledge please?

Thanks.

Upvotes: 3

Views: 6499

Answers (2)

sbarzowski
sbarzowski

Reputation: 2981

I see two options:

a) You add a variable part to your URL. Something like that:

@planck.route("/admin/assign_ticket/<int:ticket_id>")
def assign_to(ticket_id):
   some_work()

I think this is a more Flask way to approach this issue, but it will require different url from the one mentioned in the question. Instead it will be like: http://planck:5000/admin/assign_ticket/12

b) Just:

from flask import request

And request.args is a dict with your url query parameters.

from flask import request

@planck.route("/admin/assign_ticket")
def assign_to(): 
    ticket_id = request.args.get('ticket_id')
    some_work()

Be careful to properly handle case when such parameter is not passed.

Upvotes: 5

tourdownunder
tourdownunder

Reputation: 1827

There is a dict called args in request with all query string parameters.

from flask import request

@planck.route("/admin/assign_ticket")
    def assign_to(): 
        ticket_id = None
        if 'ticket_id' in request.args:
             ticket_id = request.args['ticket_id']

Upvotes: 5

Related Questions