0

I would like to use a variable outside of the function in which it was defined. I have only ever passed data to different templates and have not had to reference that data afterwards so I am unsure as to how I should proceed.

views.py

def new_opportunity_company_id(request):
    company = request.GET.get('selected_company')
    company_obj = cwObj.get_company(company)
    company_id = company_obj[0]['id']
    return company_id


def new_opportunity_location(request):
    for company_id in new_opportunity_company_id(request):
        locations = cwObj.get_sites(company_id)
        context = {'locations': locations}
    return render(request, 'website/new_opportunity_location.html', context)

Any help would be greatly appreciated, I am still new to Django/Python. Thanks!

1 Answer 1

1

Which variable are you referring to in this example? Also note your code is assuming that company_obj[0]['id'] is a list as your trying to iterate it in new_opportunity_location. The times you should be attempting to access a variable outside of a function scope do not come that often. They would either be a global, class variable or a function parameter thats passed. Other then that you may need to reconsider your approach to make your code more straight forward.

Following up to your comment...

 def new_opportunity_location(request):
        company = request.GET.get('selected_company')
        company_obj = cwObj.get_company(company)
        company_id = company_obj[0]['id']
        locations = cwObj.get_sites(company_id)
        context = {'locations': locations}

        return render(request, 'website/new_opportunity_location.html', context)

I imagine you had something close to this? A Reference before assignment means your trying to access a variable that hasn't been set yet. So this probably means that line company_id = company_obj[0]['id'] returned none and then trying to use it in cwObj.get_sites(company_id) caused a reference error

Sign up to request clarification or add additional context in comments.

7 Comments

I want to access company_id and pass that value as a parameter in cwObj.get_sites(company_id). I originally tried to handle this within the same function but ran into a local variable referenced before assignment error.
@garmars please see my updated answer it was easier to format
@cuylery quint Yeah that is exactly right, how should I go about assigning the variable before it is called then?
@garmars what is cwObj? It is similarly undefined in your original code.
@garmars based off the code i presented each line is dependent on the one above itself. So you could have an issue with ` company_obj = cwObj.get_company(company)` being None as well. Personally what I would do is seeing the value of each variable after each line by printing e.i. print(company_obj), print(company_id) . after you try to set each of these variables. This will give you more insight to what your assuming is happening but currently isn't. Ive lost count the amount of times I've debugged with print()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.