I trying to build simple web application, while doing I am able to get the data from form to flask but while sending back or to another html form data is not going to textbox. Could anyone please help?
I am taking the mobile number in the index page checking if exists if not going to create screen, there the mobile num is not getting populated in the textbox. But I am able to send the data to message.
app.py
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from send_mail import send_mail
app = Flask(__name__)
ENV = 'dev'
if ENV == 'dev':
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:Password-1@localhost/lexus'
else:
app.debug = False
app.config['SQLALCHEMY_DATABASE_URI'] = ''
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Feedback(db.Model):
__tablename__ = 'feedback'
id = db.Column(db.Integer, primary_key=True)
mobile_num = db.Column(db.BigInteger, unique=True)
cus_name = db.Column(db.String(200))
aadhar = db.Column(db.BigInteger)
cus_address = db.Column(db.Text())
def __init__(self, mobile_num, cus_name, aadhar, cus_address):
self.mobile_num = mobile_num
self.cus_name = cus_name
self.aadhar = aadhar
self.cus_address = cus_address
with app.app_context():
db.create_all()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/submit', methods=['POST','GET'])
def submit():
if request.method == 'POST':
mobile_num = request.form['mobile_num']
cus_name = request.form['cus_name']
aadhar = request.form['aadhar']
cus_address = request.form['cus_address']
cus_btn = request.form['cus_btn']
#print(cus_btn)
if cus_btn=='Search':
if mobile_num == '':
return render_template('index.html', message='Please enter required fields:')
if db.session.query(Feedback).filter(Feedback.mobile_num == mobile_num).count() == 0:
return render_template('create.html',message=mobile_num)
else:
qry_result = db.session.query(Feedback).filter(Feedback.mobile_num == mobile_num).all()
for feedback_entry in qry_result:
print(feedback_entry.cus_name)
return render_template('index.html', mobile_num=feedback_entry.mobile_num,cus_name=feedback_entry.cus_name,aadhar=feedback_entry.aadhar,cus_address=feedback_entry.cus_address)
@app.route('/create', methods=['POST','GET'])
def create():
if request.method == 'POST':
mobile_num = request.form['mobile_num']
cus_name = request.form['cus_name']
aadhar = request.form['aadhar']
cus_address = request.form['cus_address']
data = Feedback(mobile_num, cus_name, aadhar, cus_address)
db.session.add(data)
db.session.commit()
#send_mail(mobile_num, cus_name, aadhar, cus_address)
return render_template('index.html')
if __name__ == '__main__':
app.run()
```python
create.html
TRC Group
{% if message %}
{{ message | safe }}
{% endif %}
Mobile Number
Name:
aadhar
cus_address
I tried changing to GET but still not able to populate. Data is getting passed to message field but not for the textbox.
Navinkumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.