NameError: global name 'query' is not defined
<p>I have a small django project and Im trying to pass a variable from my views.py into tasks.py and run a task using the variable, but I am getting name is not defined error, ive tried many solutions ive seen on other questions but i cannot seem to get it to work</p>

<p>here is my views.py</p>

<pre><code># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render, loader
from django.template import Context
from django.http import HttpResponse
import json
import requests
from tasks import rti

def index(request):
    return render(request, 'bus/index.html')

def search(request):
    query = request.GET.get('q')
    t = loader.get_template('bus/search.html')
    c = Context({ 'query': query,})
    rti()
    return HttpResponse(t.render(c))
</code></pre>

<p>here is my tasks.py</p>

<pre><code>from background_task import background
import time


@background(schedule=1)
def rti():
    timeout = time.time() + 60 * 15
    while time.time() &lt; timeout:
        from views import search
        dblink = '*apiurl*' + str(query) + '&amp;format=json'
        savelink = 'bus/static/bus/stop' + str(query)+ '.json'
        r = requests.get(dblink)
        jsondata = json.loads(r.text)
        with open(savelink, 'w') as f:
            json.dump(jsondata, f)
</code></pre>

<p>here is the traceback:</p>

<pre><code>Traceback (most recent call last):
  File "/Users/dylankilkenny/dev/python/test2/lib/python2.7/site-packages/background_task/tasks.py", line 49, in bg_runner
    func(*args, **kwargs)
  File "/Users/dylankilkenny/dev/python/test2/mysite/bus/tasks.py", line 9, in rti
    from views import search
NameError: global name 'query' is not defined
</code></pre>

<p>You need to modify your task so that it takes the query as an argument.</p>

<pre><code>@background(schedule=1)
def rti(query):
    ...
</code></pre>

<p>Then pass the query when you call the task in your view</p>

<pre><code>rti(query)
</code></pre>

python django