TutorialsPoint - Flask – SQLAlchemy not working

2024/11/24 22:39:00

I did all that was stated on tutorial point (just copied and pasted), but when I tried to add a student entry,i.e. ‘Add Student’ it gives

Bad Request The browser (or proxy) sent a request that this server could not understand.

Please advise if there is anything wrong with the tutorial.

It failed at this line within def new(), in app.py:

 student = students(request.form['name'], request.form['city'], request.form['addr'], request.form['pin'])

Whoever is flagging this down. Note that it is the tutorial that is filled with typos and wrong indentations. I am only a student. Shut this down and I will learn nothing.

Ref: http://www.tutorialspoint.com/flask/flask_sqlalchemy.htm

from flask import Flask, request, flash, url_for, redirect, render_template
from flask_sqlalchemy import SQLAlchemyapp = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'
app.config['SECRET_KEY'] = "random string"db = SQLAlchemy(app)class Students(db.Model):id = db.Column('student_id', db.Integer, primary_key = True)name = db.Column(db.String(100))city = db.Column(db.String(50))addr = db.Column(db.String(200)) pin = db.Column(db.String(10))def __init__(self, name, city, addr,pin):self.name = nameself.city = cityself.addr = addrself.pin = pin@app.route('/')def show_all():return render_template('show_all.html', Students = Students.query.all() )@app.route('/new', methods = ['GET', 'POST'])def new():if request.method == 'POST':if not request.form['name'] or not request.form['city'] or not request.form['addr']:flash('Please enter all the fields', 'error')else:print "1";student = Students(request.form['name'], request.form['city'], request.form['addr'], request.form['pin'])print "1";db.session.add(student)print "1";db.session.commit()print "1";flash('Record was successfully added')print "=======>>>>>>>>>";return redirect(url_for('show_all'))return render_template('new.html')if __name__ == '__main__':db.create_all()app.run(debug = True)
Answer

The tutorial has an indentation problem in the student class. The constructor code should be indented one level so it becomes a method of the student class.

Corrected code: (note the indent of "def init(self, name, city, addr,pin):" in the code below)

class students(db.Model):id = db.Column('student_id', db.Integer, primary_key = True)name = db.Column(db.String(100))city = db.Column(db.String(50))addr = db.Column(db.String(200)) pin = db.Column(db.String(10))def __init__(self, name, city, addr,pin):self.name = nameself.city = cityself.addr = addrself.pin = pin

The reason is, if that indent is not there, python will not see this function as a constructor of the student class. So the constructor with the matching number of arguments is not found, resulting in the error.

https://en.xdnf.cn/q/120484.html

Related Q&A

Implement f-string like magic to pimp Djangos format_html()

I would like to pimp format_html() of Django. It already works quite nicely, but my IDE (PyCharm) thinks the variables are not used and paints them in light-gray color:AFAIK f-strings use some magic re…

Selecting a set of numbers from a list which add up to a given value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

List of even numbers at even number indexes using list comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

How do I check if a list of lists exists in a list of lists?

I got a list of lists b and I want to check if they exist in list a which is also a list of lists. Im currently using the following method which is quite time-consuming. Is there a faster way? b = [[…

How do i print out a number triangle in python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Real time clock display in Tkinter

I want to create real time clock using Tkinter and time library. I have created a class but somehow I am not able to figure out my problem.My codefrom tkinter import *import timeroot = Tk()class Clock:…

cffi export python code to dll , how to read image object in @ffi.def_extern()

i am trying to convert my python code to dll using cffi so i can access this code in my c# apllication, and i am trying to send image my c# code to python function, below is my code to read the file an…

Python 2.7.5 and Python 3.6.5

I have installed Python 3.6.5 however when i type Python it shows Python 2.7.5. Id like to use Python 3.[aravind@aravind05 Python-3.6.5]$ python3 --version Python 3.6.5[aravind@aravind05 Python-3.6.5]$…

How use creating polynomial expression like function in Python?

Id like to write a program in Python where user define a deegre of polynomial and coefficients (a,b,c). When program create a polynomial expression with this data Id like to use it like function becaus…

how to merge two sublists sharing any number in common? [duplicate]

This question already has an answer here:Using sublists to create new lists where numbers dont repeat(1 answer)Closed 9 years ago.Given thatg=[[1,2,3,4],[4,5,6],[6,7],[10,11]]What code should I use to …