How to get checked multiple radio input value in flask? - python

I am making a quiz app. I want to send back the values of checked radio inputs after form has been submitted.Howto send the values in dict format. like this: { 1:A,2:B...}
FLASK
app.route('/test', methods=['GET','POST'])
#is_logged_in
def test():
if request.method == 'POST':
app.logger.info(request.form)//What to do here?
#create cursor
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM questions")
data = cur.fetchall()
return render_template('quiz.html', data=data)
HTML
<form action="" class="text-left ml-4" method="POST">
{% for i in data %}
<h5>{{i['id']}}. {{i['question']}}</h5>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="option" value="A"
> {{i['chA']}}
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="option" value="B">{{i['chB']}}
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="option" value="C" > {{i['chC']}}
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="option" value="D" > {{i['chD']}}
</label>
</div>
<br>
{% endfor%}
<div class="col-md-12 text-center mb-1">
<button type="submit" class="btn-new btn-lg"> Submit </button>

Related

Weasyprint render_to_string <weasyprint.HTML object at 0x7f6f944df190> 2 extra bytes in post.stringData array problem

I have an html template and I want to print a pdf with weayprint for it. However, I am getting the error "<weasyprint.HTML object at 0x7f6f944df190> 2 extra bytes in post.stringData array problem" that I mentioned in the title. I am sharing my views.py file and some of my html template.
views.py
def export_pdf(id):
print('export')
edit_ambulanceCase = CallCenter.objects.all()
html_string = render_to_string('forms/update_forms/pdf.html',{'PreCaseControl':edit_ambulanceCase})
print(html_string)
html = HTML(string=html_string)
print(html)
return HttpResponse(html.write_pdf('deneme.pdf'))
pdf.html
<main>
<div class="container-fluid px-4">
<div class="row justify-content-center">
<div class="col-lg-12">
<div class="card shadow-lg border-0 rounded-lg mt-5">
<div class="card-header"><h4 class="text-center font-weight-light my-4">Çağrı Merkezi Formu</h4></div>
<div class="card-body">
<form action="" method="POST">
{% csrf_token%}
<h3>Çağrıyı Yapan</h3>
<div class="row">
<div class="mb-3 col">
<label for="institution_name" class="form-label">KURUM ADI</label>
<input type="text" class="form-control" id="institution_name" name="institution_name" value="{{CallCenter.institution_name}}">
</div>
<div class="mb-3 col">
<label for="caller_username" class="form-label">ADI SOYADI</label>
<input type="text" class="form-control" id="caller_username" name="caller_username" value="{{CallCenter.caller_username}}">
</div>
</div>
<div class="row">
<div class="mb-3 col">
<label for="proximity" class="form-label">YAKINLIĞI</label>
<input type="text" class="form-control" id="proximity" name="proximity" value="{{CallCenter.proximity}}">
</div>
<div class="mb-3 col" data-type="control-phone">
<label for="caller_tel_no" class="form-label">TELEFON NUMARASI</label>
<input type="tel" class="form-control" id="caller_tel_no" name="caller_tel_no" value="{{CallCenter.caller_tel_no}}" data-component="phone" inputmode="text" maskvalue="(###) ###-####" val>
<label class="form-sub-label" style="min-height: 13px;" for="caller_tel_no_sub">Lütfen geçerli bir telefon numarası girin.</label>
</div>
</div>
<h3>Hasta</h3>
<div class="row">
<div class="mb-3 col">
<label for="patient_username" class="form-label">ADI SOYADI</label>
<input type="text" class="form-control" id="patient_username" name="patient_username" value="{{CallCenter.patient_username}}">
<label for="patient_age" class="form-label">YAŞ</label>
<input type="text" class="form-control" id="patient_age" name="patient_age" value="{{CallCenter.patient_age}}">
</div>
<div class="mb-3 col">
<label for="" class="form-label">CİNSİYET</label>
<div >
<input class="form-check-input" type="radio" id="male" name="gender" {% if CallCenter.gender == 'erkek' %} checked {%endif%} value="erkek">
<label for="male" class="form-label">Erkek</label>
</div>
<div>
<input class="form-check-input" type="radio" id="female" name="gender" {% if CallCenter.gender == 'kadın' %} checked {%endif%} value="kadın">
<label for="female" class="form-label">Kadın</label>
</div>
</div>
</div>
My html template continues as I have attached. It does not contain any other content. I couldn't include the whole thing because it was too long.

How to get/Fetch Form data in django using cloud firestore?

Form picture
ID is been selected but I'm not getting the values in the form, please check my code files. See in the picture in URL of the browser, update/id is selected, the problem is values are not fetched in the form.
HTML:
<form id="task-form" name="myForm">
{% csrf_token %}
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-building" placeholder="Building name" name="building" value="{{buildings.building}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-postal" placeholder="Postal Code" name="postalCode" value="{{buildings.postalCode}}">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-town" placeholder="town" name="town" value="{{buildings.town}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-street" placeholder="Street" name="street" value="{{buildings.street}}">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" class="form-control" id="task-house" placeholder="House No." name="houseNo" value="{{buildings.houseNo}}">
</div>
<div class="col">
<input type="text" class="form-control" id="task-info" placeholder="Additional Information" name="additionalInfo" value="{{buildings.additionalInfo}}">
</div>
</div>
</div>
<div class="text-center mt-3">
<button type="submit" id="btn-task-form" class="btn btn-primary ">UPDATE</button>
</div>
</form>
views.py
def update_Building(request, id):
docId = id;
context = {
'buildings': db.collection('Buildings').document(docId).get()
}
return render(request,"EmployeeAdmin/updateBuilding.html", context)
urls.py
path('update/<str:id>/',views.update_Building,name='update_Building'),

How do I get images to show on my django app?

On my Django app, I am trying to make an eBay type of site that lets people post listings with pictures and bid on them. Everything works except posting an image. The image won't show and I do not know if it's the HTML or the python. If you have any questions let me know.
Html code:
{% extends "auctions/layout.html" %}
{% block body %}
<div class="container">
<h2>Create listing</h2>
</div>
<div class="container">
<form action="{% url 'submit' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlInput1">Title</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Title of the lisiting..." name="title" required>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" placeholder="Description..." name="description" required></textarea>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Category</label>
<select class="form-control" id="exampleFormControlSelect1" name="category" required>
<option>Fashion</option>
<option>Tools</option>
<option>Toys</option>
<option>Electronics</option>
<option>Home accessories</option>
<option>Books</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Initial Bid</label>
<input type="number" class="form-control" id="exampleFormControlInput1" placeholder="Starting bid..." name="price" required>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Image link (optional)</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="https://blah-blah.jpg..." name="link">
</div>
<button class="btn btn-outline-info" type="submit">Submit</button>
</form>
</div>
{% endblock %}
This is the python function for making listings:
def create(request):
try:
w = Watchlist.objects.filter(user=request.user.username)
wcount=len(w)
except:
wcount=None
return render(request,"auctions/create.html",{
"wcount":wcount
})

Why am I getting Bad request error for my flask code? [duplicate]

This question already has answers here:
Post values from an HTML form and access them in a Flask view
(2 answers)
Closed 4 years ago.
I have a code like this. This is giving me 400 Bad request error. I found that there is only one form in the page from which the form is submitted.
#auth.route('/admin/project/add',methods = ['POST', 'GET'])
def addproject():
if request.method == 'POST':
projectname = request.form['projectname']
c, conn = connection()
query = "SELECT id from projects WHERE UPPER(project)='{}'".format(projectname)
c.execute(query)
value = c.fetchall
if value>0:
return render_template('addproject.html')
else:
flash("Project already exists. Please goto projects page and confirm. If it is and error, please contact devloper.")
return redirect(url_for('addproject'))
else:
return render_template('addproject.html')
I am also adding my HTML code for the form below. I tried a lot and is not able to figure out why I am keeping on getting that error. I also did small changes in my views.py,still the result is same.
<form action="/admin/project/add" id="myForm" method="POST" name="add">
<div class="form-body">
<div class="row p-t-20">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Project Name</label>
<input type="text" id="projectname" class="form-control" placeholder="Please enter project name" required >
</div>
</div>
<!--/span-->
<div class="col-md-6">
<div class="form-group has-danger">
<label class="control-label">Project Location</label>
<input type="text" id="projectlocation" class="form-control form-control-danger" placeholder="Please enter project location" required >
</div>
</div>
<!--/span-->
</div>
<!--/row-->
<div class="row">
<!--/span-->
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Starting Date</label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" required >
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">End Date</label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" required >
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group has-danger">
<label class="control-label">Leader</label>
<input type="text" id="lead" class="form-control form-control-danger" placeholder="Please enter the leader for the project" required >
</div>
</div>
<div class="col-md-6">
<div class="form-group has-danger">
<label class="control-label">Current Status</label>
<select class="form-control" required>
<option value="">--Select--</option>
<option value="T">T</option>
<option value="D">D</option>
<option value="S">S</option>
</select>
</div>
</div>
</div>
<!--/span-->
<!--/span-->
</div>
<!--/row-->
<h5 class="box-title m-t-40">Project Description (optional)</h5>
<hr>
<div class="col-md-6">
<div class="form-group has-danger">
<TEXTAREA rows="8" cols="138"></TEXTAREA>
</div>
</div>
<!--/span-->
</div>
</div><div class="col-md-6">
<div class="form-actions">
<button type="submit" class="btn btn-success" name="add"> <i class="fa fa-plus" value="Add"></i> Add</button>
<button type="button" class="btn btn-inverse" onclick="viewproject();" name="cancel" value="Cancel">Cancel</button>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li<{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</div></div>
</form>
The answer to your question is in the addproject.html file and the way that sends the parameter.
To solve it check for mistakes in the name of the parameter ('projectname'). See if the name is the same in the addproject.html file.
Another common problem is set incorrectly the 'content-type'.
After you edit your question with the form code, I saw you are using input id instead input name in the form.
The correct way is:
<input type="text" name="projectname" ... >
I hope this helps!

html - invalid value of multi input radio button

I have multi input type radio buttons on my page and when I submitted it, on the server side, that two values are exchanged, I have no idea how to debug it.
So here my code,
<div id="is_partial_time" class="form-group">
<label class="col-md-3 col-sm-4 control-label" for="is_partial_time">Would you accept a CDD or a replacement contract</label>
<div class="col-md-7 col-sm-8">
<div id="group_is_partial_time" class="btn-group" data-toggle="buttons">
<label class="btn btn-info">
<input type="radio" name="is_partial_time" value="true"/>Yes
</label>
<label class="btn btn-info active">
<input type="radio" name="is_partial_time" value="false" checked="checked"/>No
</label>
</div>
</div>
</div>
<div id="is_replacement_contract" class="form-group">
<label class="col-md-3 col-sm-4 control-label" for="is_replacement_contract">Would you accept a partial time?</label>
<div class="col-md-7 col-sm-8">
<div id="group_is_replacement_contract" class="btn-group" data-toggle="buttons">
<label class="btn btn-info">
<input type="radio" name="is_replacement_contract" value="true"/>Yes
</label>
<label class="btn btn-info active">
<input type="radio" name="is_replacement_contract" value="false" checked="checked"/>No
</label>
</div>
</div>
</div>
Example,
on the web form ():
I choose:
Would you accept a replacement contract? Yes
Would you accept a partial time? No
On the server side:
Would you accept a replacement contract? No
Would you accept a partial time? Yes
Info: I'm using python Flask.
Thank you.
Your <label ..> are at the wrong place. It should be
<div id="is_partial_time" class="form-group">
<label class="col-md-3 col-sm-4 control-label" for="is_partial_time">Would you accept a partial time?</label>
<div class="col-md-7 col-sm-8">
<div id="group_is_partial_time" class="btn-group" data-toggle="buttons">
<label class="btn btn-info">
<input type="radio" name="is_partial_time" value="true"/>Yes
</label>
<label class="btn btn-info active">
<input type="radio" name="is_partial_time" value="false" checked="checked"/>No
</label>
</div>
</div>
</div>
<div id="is_replacement_contract" class="form-group">
<label class="col-md-3 col-sm-4 control-label" for="is_replacement_contract">Would you accept a CDD or a replacement contract</label>
<div class="col-md-7 col-sm-8">
<div id="group_is_replacement_contract" class="btn-group" data-toggle="buttons">
<label class="btn btn-info">
<input type="radio" name="is_replacement_contract" value="true"/>Yes
</label>
<label class="btn btn-info active">
<input type="radio" name="is_replacement_contract" value="false" checked="checked"/>No
</label>
</div>
</div>
</div>

Categories