2

Is there a function like trim() in python?

i use Flask miniframework and it doesn't accept:

selected_student = (request.args.get('student_form')).strip()

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


selected_student.replace(" ", "")

its error: AttributeError: 'NoneType' object has no attribute 'replace'


i need a function like trim() without coding a class/subclass or javascript

5
  • 4
    request.args.get('student_form') returned None, not a string, because the field student_form is not there. You could return a default '' instead with request.args.get('student_form', '') but you probably should check why the field is not there in the first place. Commented Nov 30, 2017 at 8:10
  • How are you sending your form data to your Flask file? Could you add an example to clarify how you're doing this? Commented Nov 30, 2017 at 8:30
  • 1
    @AJC24 <form action="student" method='POST'> <select name="student_form "> {% for student in students_list %} <option value="{{student}}">{{student}}</option> {% endfor %} </select> <input type='submit' value='submit'> </form>and here is .py:... with app.open_resource('static/docs/student3.txt') as f3: content3 = f3.read()... and: ... elif (selected_student == students_list[3]): content_selected = content3 ... i delete smthing, i think its clear. and i'm not sure is there any blank character in form data... Commented Nov 30, 2017 at 8:40
  • Just pointing this out - but you have a possible typo in your form action="student". Your action should be a URL and should read as /student. I assume you've set up your Flask server to include the route /student, then, too? Commented Nov 30, 2017 at 8:46
  • Yeah, that's rigth Commented Nov 30, 2017 at 9:02

2 Answers 2

1

You're seeing the errors that you are seeing because there is no data being passed from your form to the Flask server. Your use of request is returning a None value type as opposed to a str.

You posted the following HTML mark up for your form:

<form action="/student" method='POST'>
    <select name="student_form ">
        {% for student in students_list %}
            <option value="{{student}}">{{student}}</option>
        {% endfor %}
    </select>
<input type='submit' value='submit' />
</form>

So therefore you're going to need somewhere for Flask to pick up this data on the server side, for example:

@app.route('/student', methods=['POST'])
def receive_student_form_data:
    my_selection = str(request.form.get('student_form')).strip()
    print(my_selection)

Just to clarify why I've made my method in this way: I notice that you're using request.args.get() in order to retrieve the value sent by the form. This is incorrect.

request.args is used to retrieve key / value pairs from the URL.
request.form is used to retrieve key / value pairs from a HTML form.

So I'd suggest that you should use request.form.get('student_form') instead. If you really want to be certain that it is being cast as a str when retrieved by your Flask server, then you can cast it as a str as follows:

str(request.form.get('student_form'))

Then, as has been suggested by a few people already, you can use the .strip() method to remove any trailing spaces.

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

Comments

1

There is a strip() method. The error you get is because you are trying to run it on a NoneType object. You need to run it on a string object.

>>> s = 'some string   '
>>> s.strip()
'some string'

There is also replace for strings:

>>> s.replace('some', 'many')
'many string   '

The issue you encounter is related to something else. You end with a None object instead of what you are trying to get.

in my project there is an error when i use strip() -> AttributeError: 'NoneType' object has no attribute 'strip'
This will emphasize what I mean. Run this code first: selected_student = (request.args.get('student_form')) and then try to print selected_student to get a print what the object is.
i tried content_selected = 'Printing file is failed'.strip(); but it doesn't work... and i tried print selected_student it doesn't print....
You don't need to put ; at the end of the statement in python
i forgot it, sorry :)

Your Answer

Draft saved
Draft discarded

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.