1

I have a form which takes a list of inputs via html (searchinput) and adds it to a query in python.

    searchterms = []
    searchterms = request.values.get('searchinput').strip().split(', ')
    filtered = pd.concat([df.query("Drug.str.contains('|'.join(@searchterms), na=False, case=False, regex=True)", engine='python')])
    return render_template('drugsafety.html', tables=[filtered.to_html(classes='data')], titles=['na', 'Drug List'])

This gives

AttributeError: 'NoneType' object has no attribute 'strip'

If I then feed in a value like searchterms = ["foobar"] it works and will even take the form inputs without error.

How can I get around this initial needing a value to boot it into life?

edit

    searchterms = []
    if searchterms is not None:
        searchterms = request.values.get('searchinput').strip().split(', ')
        filtered = pd.concat([ df.query("Drug.str.contains('|'.join(@searchterms), na=False, case=False, regex=True)", engine='python')])            
    else:  
        searchterms = ["Foo"]

Thanks for any help

2
  • 1
    It means that no value "searchinput" was found and get defaults to None instead. Commented Jun 18, 2021 at 8:32
  • Do you know of a way I can fix it? Putting a not none check doesnt change anything. Commented Jun 18, 2021 at 9:33

1 Answer 1

1

Try to put default value ('') in .get:

# put `''` in .get():
searchterms = request.values.get('searchinput', '').strip().split(', ')
Sign up to request clarification or add additional context in comments.

Comments

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.