how to show bootstrap modal on rendering the same page in flask - python

I have created a web application using flask and bootstrap. In this application, after putting the inputs and clicking the submit button I want to show the output or result in the same page in a bootstrap modal. But I could not redirect it properly. If there is any solution to it tell me. It will be very helpful. Thanks in advance. The codes are given below.
home.html
<div class="container" align="left">
<div class="panel panel-default" style="border:none;border-radius: 10px;">
<div class="panel-body panel1">
<form method="POST" action="/predict">
<div class="row">
<div class="col-sm-6">
<div class="form-group d2">
<label for="NewYork"><p class="p2">New York</p></label>
<input type="text" class="i1" name="NewYork"><br><br>
<small id="NYHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
</div>
<div class="form-group d2">
<label for="california"><p class="p2">California</p></label>
<input type="text" class="i1" name="California"><br><br>
<small id="CaliforniaHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
</div>
<div class="form-group d2">
<label for="Florida"><p class="p2">Florida</p></label>
<input type="text" class="i1" name="Florida"><br><br>
<small id="FloridaHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
</div>
</div>
<div class="col-sm-6">
<div class="form-group d2">
<label for="RnD_Spend"><p class="p2">RnD_Spend</p></label>
<input type="text" class="i1" name="RnD_Spend"><br><br>
<small id="RndSpendHelp" class="form-text text-muted s1">Enter RnD Spendings in US Dollar</small>
</div>
<div class="form-group d2">
<label for="Admin_Spend"><p class="p2">Admin_Spend</p></label>
<input type="text" class="i1" name="Admin_Spend"><br><br>
<small id="AdminSpendHelp" class="form-text text-muted s1">Enter Adminstration Spendings in US Dollar</small>
</div>
<div class="form-group d2">
<label for="Market_Spend"><p class="p2">Market_Spend</p></label>
<input type="text" class="i1" name="Market_Spend"><br><br>
<small id="MarketSpendHelp" class="form-text text-muted s1">Enter Market Spendings in US Dollar</small>
</div>
</div>
</div>
<div class="row" align="right"><input class="btn btn1" type= "submit" value="Submit" data-toggle="modal" data-target="#myModal"></div>
</form>
</div>
</div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
app.py
#app.route("/predict",methods=['GET', 'POST'])
def predict():
NewYork = float(request.form['NewYork'])
California = float(request.form['California'])
Florida = float(request.form['Florida'])
RnD_Spend= float(request.form['RnD_Spend'])
Admin_Spend= float(request.form['Admin_Spend'])
Market_Spend = float(request.form['Market_Spend'])
pred_args =[NewYork, California, Florida, RnD_Spend, Admin_Spend, Market_Spend]
pred_args_arr = np.array(pred_args)
pred_args_arr = pred_args_arr.reshape(1, -1)
mul_reg = open("multiple_linear_model.pkl", "rb")
ml_model = joblib.load(mul_reg)
model_prediction = ml_model.predict(pred_args_arr)
model_prediction = round(float(model_prediction), 2)
return render_template('home.html', prediction = model_prediction)

I would say that it is usually not a good practice to redirect a user after form submission to the same page but with a modal shown (it is just not a good user experience), and I would recommend doing this through the use of AJAX.
Anyway, you can pass a flag that tells the HTML template renderer if it needs to show the modal. Something like this:
return render_template(
'home.html',
prediction=model_prediction,
show_predictions_modal=True // This is our flag
)
Then conditionally render your modal in the template:
{% if show_predictions_modal %}
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
{% endif %}

Related

Receive array from HTML form in FastAPI

I'm trying to submit a form through post in FastAPI and get a form variable that is stored as a list. When I read back the value all I get is an empty list. There are several variables that will eventually be in list form but all I am trying to get at the moment is the array that holds the quantity values.
What am I doing wrong?
Here is my code:
Python Code
#app.post("/edit_bom")
def edit_bom(board_id: int = Form(...), quantity: list = Form(...)):
print("Editting BOM, board ID: " + str(board_id))
print("quantity: ", quantity)
return RedirectResponse(url=f"/get_bom_details/" + str(board_id), status_code=303)
HTML Code
<form method="post" action="/edit_bom" id="parent-form">
<div class="mb-3 row">
<div class="d-grid gap-2 col-2">
<button type="button" class="btn btn-outline-primary add-one-more"><i class="fas fa-fw fa-plus-square"></i> New Row</button>
</div>
<div class="d-grid gap-2 col-2">
<button type="submit" name="action" value="save-bom" class="btn btn-outline-success"><i class="fas fa-fw fa-check"></i> Save</button>
</div>
</div>
<div class="mb-3 row" id="header">
<label class="col-form-label col-sm-1 text-sm-end"></label>
<div class="col-sm-7">
<h3>Part Number</h3>
</div>
<div class="col-sm-2">
<h3>Quantity</h3>
</div>
<div class="input-group col">
<h3>Edit</h3>
</div>
<div class="input-group col">
<h3>Delete</h3>
</div>
</div>
<div class="mb-3 row bom-line-item copy-fields" id="entry">
<label class="col-form-label col-sm-1 text-sm-end child-row-label">1</label>
<div class="col-sm-7 child-outer">
<input type="text" name="part-name_1" id="0" class="form-control child-part-name" placeholder="Board Number" value="ISL73847SEHDEMO1ZB">
</div>
<div class="col-sm-2 child-outer">
<input type="number" name="quantity[]" id="0" class="form-control child-part-quantity" placeholder="Enter quantity" value="1">
</div>
<div class="input-group col child-outer">
<button id="btn-edit_1" class="btn btn-primary child-btn-edit" type="button"><i class="fas fa-fw fa-edit"></i></button>
</div>
<div class="input-group col child-outer">
<button id="btn-del_1" class="btn btn-danger child-btn-delete" type="button"><i class="fas fa-fw fa-ban"></i></button>
</div>
</div>
<div class="end-of-list" name="noOfRows" value="1"></div>
<input type="hidden" name="board_id" value="1">
</form>
My output in the command prompt:
Editting BOM, board ID: 1
quantity: []
Using the comment from #MatsLindh
I had to change the HTML element name from "quantity[]" to "quantity" and it works now.

Modal window not called after button click

I'm making my first website, using Django, python and bootstrap 5, I want a person to click the "submit" button after filling out the form and after that a modal window pops up in which it would say that "everything is OK, your application has been created"
<div class="row mt-5 mb-5 p-2">
<form action="{% url 'feedback_view' %}" method="POST">
{% csrf_token %}
<div class="col-md-6 mb-2">
<label for="exampleInputName" class="form-label">Ваше Имя</label>
<input name="name" class="form-control" id="exampleInputName" aria-describedby="NameHelp">
</div>
<div class="col-md-6 mb-2">
<label for="exampleInputphone" class="form-label">Ваш Телефон</label>
<input name="phone" class="form-control" id="exampleInputphone" aria-describedby="NameHelp" placeholder="+7 (918) 000-00-00">
</div>
<button type="submit" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">send</button>
</form>
</div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">send_btn</button>
but for some reason the "send" button does not work, but the "send_btn" button works, which I made to check the problem. all I understand at the moment is what bothers me, type="submit" because in the afternoon there is type="button". How can I solve this problem, given that this is a form submission button? this is the window code:
{% if messages %}
{% for message in messages %}
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p class="reviews">{{ message }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
{% endfor %}
{% endif %}
it's views.py
class FeedBackView(View):
def post(self, request):
form = FeedBackForm(request.POST)
if form.is_valid():
form.save()
messages.add_message(request, settings.MY_INFO, 'now call you!')
else:
messages.add_message(request, settings.MY_INFO, 'something was wrong')
return redirect("/")

how to make card-text and horizontal list group items editable in a bootstrap card?

Using bootstrap card-text and horizontal list group I was able to show information in a bootstrap card, but I want them to be editable like bootstrap 4 form. So, how I will be able to do that? I have provided the code that is showing information in a bootstrap 4 card.
<div class="personal">
<div class="card">
<div class="card-body">
<h5 class="card-title">Personal</h5>
<p class="card-text">Your Personal Inforation</p>
<ul class="list-group list-group-horizontal">
<li class="list-group-item">Mobile Phone</li>
<li class="list-group-item">{{employee.phone_number}}</li>
</ul>
<ul class="list-group list-group-horizontal">
<li class="list-group-item">Birth Date</li>
<li class="list-group-item">{{employee.birth_date}}</li>
</ul>
<ul class="list-group list-group-horizontal">
<li class="list-group-item">Age</li>
<li class="list-group-item">age</li>
</ul>
<ul class="list-group list-group-horizontal">
<li class="list-group-item">Address</li>
<li class="list-group-item">{{employee.address}}</li>
</ul>
<ul class="list-group list-group-horizontal">
<li class="list-group-item">Gender</li>
<li class="list-group-item">{{employee.gender}}</li>
</ul>
</div>
</div>
</div>
add bootstrap model for the items editable Bootstrap models
<div class="container">
<div class="row personal">
<div class="col-12 col-md-12">
<div class="card">
<div class="card-header">
<h5 class="card-title">Personal</h5>
<h6 class="card-text">Your Personal Information</h6>
</div>
<div class="card-body">
<div class="col-12 col-md-9">
<form>
<div class="form-group row">
<label for="phone" class="col-md-2 col-form-label">Mobile Phone</label>
<div class="col-md-10">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone Number">
</div>
</div>
<div class="form-group row">
<label for="birthdate" class="col-md-2 col-form-label">Birth Date</label>
<div class="col-md-10">
<input type="date" class="form-control" id="birthdate" name="birthdate">
</div>
</div>
<div class="form-group row">
<label for="age" class="col-md-2 col-form-label">Age</label>
<div class="col-md-10">
<input type="text" class="form-control" id="age" name="age" placeholder="Your age">
</div>
</div>
<div class="form-group row align-items-center">
<label for="gender1" class="col-md-2 col-form-label">Gender</label>
<div class="col-md-10">
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="gender1" name="gender" class="custom-control-input">
<label class="custom-control-label" for="gender1">Male</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="gender2" name="gender" class="custom-control-input">
<label class="custom-control-label" for="gender2">Female</label>
</div>
</div>
</div>
<div class="form-group row">
<label for="address" class="col-md-2 col-form-label">Address</label>
<div class="col-md-10">
<textarea class="form-control" id="address" name="address" rows="4"></textarea>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Will this work for you?

How could I retrieve data from mysql in django to be shown in a modal wherein I could also update it?

What I needed to do is after clicking the view modal button, it will show the information and then there is a button also wherein could update it.Can someone advice me which is the best thing to be done. Thanks.
Models.py
class Employee(models.Model):
first_name = models.CharField(max_length=100, blank=True, null=True)
last_name = models.CharField(max_length=100, blank=True, null=True)
middle_name = models.CharField(max_length=100, blank=True, null=True)
userid = models.CharField(max_length=45, blank=True, null=True)
email_address = models.CharField(max_length=100, blank=True, null=True)
I dont have views.py yet( Dont know where to start)
Modal Code:
{% for employee in employees %}
<div class="modal fade" id="employee.employee_id_{{ employee.employee_id }}" tabindex="-1" role="dialog" style="display: none; overflow: auto;" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" >Employee</h5>
<button type="button" class="close" modalid="referral_modal" id="close_referral" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" style="overflow: auto">
<div class="col-md-15">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-md-12">
<h4>Your Profile</h4>
<hr>
</div>
</div>
<div class="row">
<div class="col-md-12">
<form>
<div class="form-group row">
<label for="username" class="col-4 col-form-label">User Name*</label>
<div class="col-8">
<input id="username" name="username" placeholder="{{ employee.userid}}" class="form-control here" required="required" type="text">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-4 col-form-label">First Name</label>
<div class="col-8">
<input id="name" name="name" placeholder="{{ employee.first_name}}" class="form-control here" type="text">
</div>
</div>
<div class="form-group row">
<label for="text" class="col-4 col-form-label">Middle Name*</label>
<div class="col-8">
<input id="text" name="text" placeholder="{{ employee.middle_name}}" class="form-control here" required="required" type="text">
</div>
</div>
<div class="form-group row">
<label for="lastname" class="col-4 col-form-label">Last Name</label>
<div class="col-8">
<input id="lastname" name="lastname" placeholder="{{ employee.last_name}}" class="form-control here" type="text">
</div>
</div>
<div class="modal-footer">
<button name="submit" type="submit" class="btn btn-primary">Update</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="">Cancel</button>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
{% endblock %}
MySQL DB
You can make your tag readonly.
example:
and then in javascript on click "update button" you can make it editable again.
document.getElementById("name").readOnly = false;

Perform login script with python for login .asp website

I am trying to write a script that is able to perform login in this website : https://du.ilsole24ore.com/utenti/Login.aspx?RURL=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2fenter.do%3fproduct%3dBIG&errurl=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2f&sitecode=BR
I already had a look to various solutions already presented somewhere on internet, but I didn't get them working.
The html source is the following
<body>
<header id="header">
<div class="container">
<div class="row-fluid">
<div class="span3">
<a href="http://www.ilsole24ore.com" id="logo" class="thumbnail">
<img src="img/logo_sole24_new.png" />
</a>
</div>
</div>
</div>
</header>
<div id="content">
<div class="container">
<div class="well well-sha">
<div class="well">
</div>
<span class="separator"></span>
<form class="form-block" id="AuthUser" method="post" action="/DU/AuthFiles/LoginCentrale.aspx" onsubmit="return ValidateForm();">
<input id="SC" type="hidden" value='BR' name="SC"/>
<input id="RURL" type="hidden" value="http://www.banchedati.ilsole24ore.com/enter.do?product=BIG" name="RURL"/>
<input id="ERRURL" type="hidden" value='/Utenti/login.aspx?RURL=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2fenter.do%3fproduct%3dBIG&SiteCode=BR' name="ERRURL"/>
<fieldset class="form_h" style=" padding-bottom:0px!important; border-bottom:0px!important; margin-bottom:0px!important;">
<div class="row-fluid">
<div class="span6 offset2">
<div class="row-fluid">
<div class="span4">
<label class="" for="username">Username / Email</label>
</div>
<div class="span8">
<div class="validation ">
<input value="" name="txtUsername" id="txtUserName" type="text"/>
</div>
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span6 offset2 ">
<div class="row-fluid">
<div class="span4">
<label class="" for="password">Password</label>
</div>
<div class="span8">
<div class="validation ">
<input value="" id="txtPassword" name="txtPassword" type="password" />
</div>
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span6 offset2">
<div class="row-fluid">
<div class="span4">
<label class="" ></label>
</div>
<div class="span8">
<div class="validation ">
<span class="btn">
<input type="checkbox" name="RememberMe" id="RememberMe" class="checkRemember" value="1" />
Rimani collegato
</span>
<button type="submit" class="btn btn-sole">
ACCEDI
</button>
</div>
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span6 offset2">
<div class="row-fluid">
<div class="span4">
<label class="" ></label>
</div>
<div class="span8">
<div class="validation ">
<a id="linkPasswordReminder" href="passwordReminder.aspx">Recupera password</a>
</div>
</div>
</div>
</div>
</div>
</fieldset>
</form>
<span class="separator"></span>
<div class="well">
<h4>Nuovo Utente?</h4>
<p>Segui passo dopo passo le indicazioni che ti saranno fornite, fino a che non ti verrà restituito sullo schermo il messaggio "Registrazione effettuata con successo".<br />
Da utente registrato potrai ritrovare le informazioni da te rilasciate, all'interno della tua scheda personale ("<a href="/Utenti/areautente/ilmioprofilo.aspx?SiteCode=BR&RURL=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2fenter.do%3fproduct%3dBIG">Il mio
profilo</a>").</p>
<p><a style="text-decoration:underline" href='/Utenti/Registrazione.aspx?SiteCode=BR&RURL=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2fenter.do%3fproduct%3dBIG&ERRURL=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2f'>Registrati adesso</a></p>
</div>
</div>
</div>
</div>
</div>
</footer>
</body>
</html>
So far, I have written this snippet of code, but it doesn't give me the right values: I should read some values inside cookies after having done login:
import requests
import cookielib
jar = cookielib.CookieJar()
login_url = 'https://du.ilsole24ore.com/utenti/Login.aspx?RURL=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2fenter.do%3fproduct%3dBIG&errurl=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2f&sitecode=BR'
acc_pwd = { 'SC':'BR',
'txtUsername':'username',
'txtPassword':'password',
'RURL':'http://www.banchedati.ilsole24ore.com/enter.do?product=BIG',
'ERRURL':'/Utenti/login.aspx?RURL=http%3a%2f%2fwww.banchedati.ilsole24ore.com%2fenter.do%3fproduct%3dBIG&SiteCode=BR'
}
r = requests.get(login_url, cookies=jar)
r = requests.post(login_url, cookies=jar, data=acc_pwd)
print r
print r.cookies
And the output I get is :
<Response [200]>
<RequestsCookieJar[]>
Where are the errors ?
Thanks

Categories