How to get the selected value from html select with django - python

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>

Related

get values from html select in views with django

I would like to get the value and even the string if possible of the selected value in my HTML file.
index.html
<form action="#" method="post">
<select id="drop1">
<option disabled selected value> -- select value -- </option>
{% for i in df %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
</form>
views.py
def index(request):
if request.method == 'POST':
selected_value = request.POST.get('drop1')
print(selected_value)
else:
return render(request, 'index.html', {'df': df.designation_list})
With this, nothing is returned. I saw that I have to use <form></form> to get an html value but I can't find what to put in the action= field.
Can you see what is missing in my code?
Thanks in advance
The action attribute specifies where to send the form-data when a form is submitted.
In your case, you have to add the url of the view that will hand that data. For example:
<form action="{% url 'index' %}" method="post">
<select id="drop1">
<option disabled selected value> -- select value -- </option>
{% for i in df %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
</form>
Note: Change the index with the name you specified in your URL pattern.

Django 3 Pass Get The Selected Value Dropdown select option

Django dropdown option selected. Get data in template and select option. My code is.
views.py
def home(request):
compType = Type.objects.all()
comptype = request.GET.get('comp_type')
context = {
'compType': compType
}
return render(request, 'compapp/dashboard.html', context)
search.html
<div class="form-group">
<label>Type</label>
<select class="form-control select2" name="comp_type" style="width: 100%;">
<!-- <option disabled="true" selected >Company Type</option> -->
{% for i in compType %}
<option value="{{ i }}" {% if request.GET.items == i %}selected{% endif %}>{{ i }}</option>
{% endfor %}
</select>
imagine you have a form, (search form), that user filled it and submitted. you received this form and made the queryset and then you want to show a page contains the search results and, also the form has its fields filled with selected values. So you have to pass that values to the results page too! 'request.GET.items' is not working ever.
what you should do is:
if the had a field named 'dr_select', in the views.py search function you should get that fields value via
selected = get_object_or_404(Type, id==int(request.GET['dr_select']))
then pass 'selected' to the results page, and then in that template you can do what you want.
{% for item in compType %}
<option value="{{ item.id }}"
{% if selected == item %}selected{% endif %}>{{ item } </option>
{% endfor %}
*note: because I used 'item.id' for 'value' in 'option' tag, so in backend I have to filter the Model by id too.

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>

Displaying results on the same page with Flask

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?

Categories

Resources