Here is the complete code of Flask-SQLite application.įrom flask import Flask, render_template, request This list.html is a template, which iterates over the row set and renders the data in an HTML table.įinally, the ‘/’ URL rule renders a ‘home.html’ which acts as the entry point of the home(): Return render_template("list.html",rows = rows) This object is passed to the list.html list(): It populates ‘rows’ as a MultiDict object containing all records in the students table. The application contains another list() function represented by ‘/list’ URL. The HTML script of result.html contains an escaping statement that displays the result of Insert operation. Return render_template("result.html",msg = msg) Message corresponding to success or error in insert operation is rendered to = )Ĭur.execute("INSERT INTO students (name,addr,city,pin) This addrec() function retrieves the form’s data by POST method and inserts in students table. The HTML script for ‘student.html’ is as follows −Īs it can be seen, form data is posted to the ‘/addrec’ URL which binds the addrec() function. It renders an HTML file containing student information new_student(): Our Flask application has three View functions.įirst new_student() function is bound to the URL rule (‘/addnew’). In this section we shall see how a Flask application interacts with SQLite.Ĭreate an SQLite database ‘database.db’ and create a students’ table in it.Ĭonn.execute('CREATE TABLE students (name TEXT, addr TEXT, city TEXT, pin TEXT)') For a detailed tutorial on using SQLite database in Python, please refer to this link. SQlite3 module is shipped with Python distribution. Python has an in-built support for SQlite.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |