Displaying results on the same page with Flask - python

I am trying to write code that will request data for a page, process it, and then show the result in a lower section of the page without refreshing.
Flask code:
if request.method == 'GET':
return render_template("dashboard.html", data_nodes=data_nodes, data_para=data_para)
elif request.method=='POST':
#c, conn = connection2()
select1 = request.form.get('node')
select2 = request.form.get('parameter')
print str(select1)
print str(select2)
#processes the data
#display the processed data on the same page
#return render_template("dashboard.html") This will refresh the page! Is there another way?
My HTML side
<form method="POST" action="{{ url_for('dashboard')}}">
<label for="node">Node</label>
<select name="node">
{% for o in data_nodes %}
<option value="{{ o.name }}">{{ o.name }}</option>
{% endfor %}
</select>
<label for="parameter">Parameter</label>
<select name="parameter">
{% for o in data_para %}
<option value="{{ o.name }}">{{ o.name }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-default" >Send</button>
</form>
Is there a way to post the processed data without having to redirect to another page?

Related

How to get the selected value from html select with django

I would like to know if it's possible to get the selected value from a select HTML without using a submit input? Because when I hit the submit, the value selected is returned but it reloads the web page and the select goes back to it's intial state.
Here is my code to visualize better:
views.py
def index(request):
id_desi = request.POST.get('drop_desi')
return render(request, 'index.html', {'designation_liste': df.designation_liste})
index.html
<form action="{% url 'DataMat-index' %}" method="POST">{% csrf_token %}
<select name="drop_desi">
<option disabled selected value> -- select value -- </option>
{% for i in designation_liste %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
<input type="submit" value="valider">
</form>

HTML element to dynamically pop up specific suggestions for field in form

I'm working on a database tool using python+flask+werkzeug and learning how to use most of it for the first time. For the "Institution" field in the template, as the user starts typing in the name I would like existing options that fit the pattern to pop up in a drop-down, then with an option to add a new one if it's not found in that list. I've found the <select> element, and I can see a way to do it with that. However, it just has hard-coded in choices with no way to type in, and there may be hundreds of institutions I wouldn't want to put all in here. I don't know the terminology for the element I'm looking for so I don't really know what to google. Any thoughts?
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Add New Member{% endblock %}</h1>
{% endblock %}
{% block content %}
<form method="post">
<label for="full_name">Full Name</label>
<input type="text" name="full_name" id="full_name" value="{{ request.form['full_name'] }}" required>
<label for="phone_number">Phone #</label>
<input type="tel" name="phone_number" id="phone_number" value="{{ request.form['phone_number'] }}">
<label for="email">Email</label>
<input type="email" name="email" id="email" value="{{ request.form['email'] }}">
<label for="institution_name">Institution</label>
<select id="institution_name" name="institution_name">
{% for institution in institution %}
<option value="{{ institution['institution_name'] }}">{{ institution['institution_name'] }}</option>
{% endfor %}}
<option value="not_found">Not found. Add new?</option>
</select>
<input type="submit" value="Submit">
</form>
{% endblock %}
There are JavaScript plugins that allow for this, such as Select2, but a simpler option could be to use a datalist element for this.
<div>
<datalist id="institution_list">
{% for institution in institution %}
<option value="{{ institution['institution_name'] }}">{{ institution['institution_name'] }}</option>
{% endfor %}}
</datalist>
<input autoComplete="on" list="institution_list"/>
</div>

Saving dropdown menu selection as a cookie in Django

I'd like to save a user selection from a dropdown menu as a cookie. I've seen this question asked before but never fully in python.
I wish to use these two cookies in running a simple piece of code so there is no need for a database to be used.
Here is my current HTML code:
<form role="form" method="post">
{% csrf_token %}
<div class="form-group">
<label for="dog-names">Choose a dog name:</label>
<select name="dog-names" id="dog-names" onchange="getSelectValue">
<option value="rigatoni">Rigatoni</option>
<option value="dave">Dave</option>
<option value="pumpernickel">Pumpernickel</option>
<option value="reeses">Reeses</option>
<option value="{{ current_name }}"> {{ current_name }}</option>
</select>
<br/>
<label>Current dog: {{ current_name }}</label>
</div>
<button type="submit" value="Submit" class="btn btn-info">Submit</button>
</form>
Python
def cookies_test(request):
template_name = 'cookies.html'
current_name = "Rigatoni" # default name
if request.method == 'GET':
if 'name' in request.COOKIES:
current_name = request.COOKIES['name']
elif request.method == 'POST':
current_name = request.POST.get('name')
response = render(request, 'test.html', {
"current_name": current_name
})
response.set_cookie('name', current_name)
return response
The python works if I give a value to {{ current_name }}
All I want is to be able to save a value from a dropdown menu in a variable so I can save it as a cookie
Any advice at all would be appreciated :)

How to access the selected option in an select tag with Django (not using Javascript)?

I have a form that has a select tag and dynamically created options for that tag as shown below. I want to access the selected tag if it is included in the POST request as part of the form in my Python/Django code:
<form action="{% url 'newprogram' %}" method="post" novalidate>
{% csrf_token %}
TODO: Add Program to School
<select name="schooloptions">
{% for school in schools %}
<option value="{{ school.name }}">{{ school.name }}</option>
{% endfor %}
</select>
<div class="form-group">
<input autofocus class="form-control" type="text" name="programname" placeholder="">
</div>
<input class="btn btn-primary" type="submit" value="Post">
</form>
I want to select the option that the user selects from the drop-down menu as follows but it does not work:
#login_required
def newprogram(request):
if request.method == "POST":
programname = request.POST["programname"]
school = request.POST["schooloptions"].selected() #This does not work and is what I need help with
schoolobj = School.objects.get("school")
school = Program(School=schoolobj, name=programname)
school.save()
return render(request, "network/index.html")
Any thoughts as to how I can access the selected option from a select HTML tag within a form?

Can't get form data in view function: Django

I need to submit a string to a view function via a dropdown menu and submit button.
in my template I have:
<form action="{% url 'exec_task' %}" method="post">
{% csrf_token %}
<select id="select_task" form="select_task" name="select_task">
{% for task in available_tasks %}
<option id="selected_task" value="{{ task }}">{{ task }}</option>
{% endfor %}
</select>
<input class="button" type="submit" value="Run Selected Task">
</form>
in my view function I have:
def exec_task(request):
if request.method == 'POST':
task = request.POST.get('select_task')
print(task)
getattr(tasks, task)(0)
return redirect('management')
The print(task) always comes out as None, which generates an error when I try to call it via getattr in the next line.
I've read through all the questions and tutorials I can find on this and I don't know what I'm doing wrong, but when I print the request.POST object, all I get is the csrf token. The QueryDict has nothing else in it.
Any ideas?
As discussed in comments please remove
form="select_task" from select tag.
So final select tag / html would be.
<form action="{% url 'exec_task' %}" method="post">
{% csrf_token %}
<select id="select_task" name="select_task">
{% for task in available_tasks %}
<option id="selected_task" value="{{ task }}">{{ task }}</option>
{% endfor %}
</select>
<input class="button" type="submit" value="Run Selected Task">
</form>

Categories

Resources