Common Flask Errors And Their Solutions
Errors I encountered while developing flask applications and their solutions.#code , #python
Let me give you a programmer’s productivity tip that will save you countless hours. Create a note in your favorite note taking application and note down all the programming errors that encounter. As you solve these errors, note down their solution too. It is highly likely that you will encounter the error again and bingo, you got the solution at hand without wasting hours googling the error again[^1].
Here are some of the errors I encountered while developing in Flask and steps I used to solve them.
The method is not allowed for the requested URL: Check if
POST is declared as one of the methods for the function. By default,
GET is the only method for a Flask function.
@app.route('/signup/', methods=['GET', 'POST']) def signup(): return
The browser (or proxy) sent a request that this server could not understand: This error occurs when the
POST method is accessing values that are not in the form. Check the spelling of form variables and the variables in the respective handler function.
<input name="title" type="text">is your HTML form variable, then the
POST method should also use the same variable:
entry_title = request.form['title']. Ref this stackoverflow discussion for more information.
Missing primary key definition in SQLAlchemy: If the model definition doesn’t contain a primary key, SQLAlchemy will throw a confusing error:
<tablename> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class. This stackoverflow discussion helped me solve it.
mysql_config not found: I got this error while installing mysql on Mac Yosemite. By following the steps mentioned in this stackoverflow discussion, I was able to install mysql.
Non-persisting session: I took a while to figure out this error, because it happened only in production. All variables stored in session would become null. The solution is to have static secret key. I had,
SECRET_KEY = uuid.uuid4()
Changing this to a static key solved the issue. (Go to python on the command line, execute this command and copy, paste the value). As usual, stackoverflow came to the rescue.
As I face and solve more errors, I will continue to update this page. If you develop in Flask, bookmark this and keep checking.
[^1]: This is not my original idea. I read this tip on the net, but not able to find out where I read it.