Luke Kenworthy
Luke Kenworthy

Reputation: 13

Flask request variable not recognized

So I am creating a function for a route in Flask to handle a POST request but Flask isn't recognizing my request variable for some reason. I have this Python code:

@app.route("/decide", methods=["GET", "POST"])
def decide():
    if request.method == "POST":
        # Ensure user has permission to modify this request
        request_id = request.form.get("job_id")
        if not request_id:
            return redirect("/")

        request = Requests.query.get(request_id)
        if not request:
            return redirect("/")

        job = Jobs.query.get(request.job_id)
        if not job:
            return redirect("/")

        job_creator = job.creator_id
        if job_creator != session["user_id"]:
            return redirect("/")

        if request.form.get("submit") == "accept":
            print("accept")

        if request.form.get("submit") == "reject":
            print("reject")
     return redirect("/")

with this HTML code: {% extends "dad.html" %}

{% if job.creator_id == session.user_id %}

{% block head %}
<title>{{job.title}}</title>
{% endblock %}

{% block body %}

<h1>{{job.title}}</h1>
<br />
<br />
{% if total > 0 %}
    <h3>There {% if total != 1%}are{% else %}is{% endif %} {{total}} request{% if total != 1%}s{% endif %} for this job</h3>
{% else  %}
    <h3>There are no requests for this job currently</h3>
{% endif %}

<br />

{% for requ in requests %}
    <form method="POST" id="form" action="/decide">
    <div class="row">
        <input type="hidden" value="{{requ.id}}" name="job_id" />
        <div class="col-md-3">
            <h3><a href="/profiles/{{requ.requester_username}}">{{requ.requester_fname}} {{requ.requester_lname}}</a></h3>
        </div>
        <div class="col-md-1">
            <button type="submit" name="submit" value="accept" id="accept" class="btn btn-success">Accept</button>
        </div>
        <div class="col-md-1">
            <button type="submit" name="ding" value="reject" class="btn btn-danger">Reject</button>
        </div>
    </div>
    </form>

    <br />
    <br />
{% endfor %}

{% endblock %}

{% endif %}

and then I get an error:

UnboundLocalError: local variable 'request' referenced before assignment

at the line:

if request.method == "POST":

All of my other routes like this work, it just doesn't recognize "request" here for whatever reason. I also imported the request object from flask, so that's not the issue. Thanks for any help that you can give!

Upvotes: 0

Views: 1893

Answers (2)

aman5319
aman5319

Reputation: 662

Actually The problem with the code is that python is getting confused with request

  1. flask has a module named request
  2. you are declaring a variable named request

    request = Requests.query.get(request_id)

and using it over here

 job = Jobs.query.get(request.job_id)

python is getting confused which request to use the variable you declared or the flask request module.

simply change the variable name to something else

Upvotes: 2

Matt Healy
Matt Healy

Reputation: 18531

It's most likely occurring because you're redefining the request variable later on in the code block.

request = Requests.query.get(request_id)

Try changing that variable name to something else so you don't have a namespace conflict.

Upvotes: 0

Related Questions