retrive data according to the dropdown list item selected, in django
This Question Helped me a lot in creating the logic for this problem.
[Django]How to return value to view after select item from dropdown list
views.py
from django.shortcuts import render
from range.models import price_range
def showobj(request):
selected_range = None
range_price = price_range.objects.all()
if request.method == "POST":
selected_range = request.POST.get("range")
range_price = range_price.filter(name=selected_range)
price = price_range.objects.order_by('name').values_list('name', flat=True)
context = {
'ranges': price,
'range_price': range_price,
'selected_range': selected_range,
}
return render(request, 'range/price_range.html', context)
price_range.html
<center>
<h1>Index</h1>
<form method="post">
{% csrf_token %}
{% if selected_range %}
{% for rest in range_price %}
<input name="price_value" value="{{ rest.price }}"></input>
{% endfor %}
{% endif %}
<select name="range">
<option selected disabled="true">Select the Price Range</option>
{% for range in ranges %}
<option value="{{ range }}" {% if selected_range == range %} selected="selected" {% endif %}>{{ range }}</option>
{% endfor %}
</select>
<input type="submit" value="Select">
</form>
</center>
Django - Unable to retrieve data in database in dropdownlist
Try this, in your select
{% if paper %}
{% for p in paper %}
<option value="{{ p.id }}">{{ p.year }} {{ p.month }} Paper {{ p.number }} </option>
{% endfor %}
{% endif %}
Also go through this tutorial, django tutorial part 3.
Also django models have a default primary key attribute you can use that also.
Select data from drop-down list and save it to database in Django
You can render choices using {% for %}
loop and FoodStatus
list of choices like this:
<td>
{{ order.get_food_status_display }}
<select name="food_status">
{% for id, choice in order.FoodStatus %}
<option value="{{ id }}"{% if order.food_status == id %} selected="selected"{% endif %}>{{ choice }}</option>
{% endfor %}
</select>
</td>
You can display actual status text (instead of id), using get_FOO_display
method.
Added {% if %}
tag to preselect correct option.
Consider switching to Forms
so it can handle rendering fields automatically.(!!!)
Consider switching food_status
to IntegerField
instead. Provide default
attribute, so it will always be one of the choices, even if not specified.
Get data from dropdown list and display it in the form of table
In your views.py
:
def save_machine(request):
if request.method == "POST":
machine_name = request.POST.get('machine_name','')
operation_no = request.POST.get('operation_no','')
choiced_machine = Machine.objects.get(machine_name=machine_name, operation_no=operation_no)
machines = Machine.objects.all()
return render(request,'usermaster/upload.html',{'machines':machines,'choiced_machine':choiced_machine})
In your urls.py
:
urlpatterns = [
path('save',views.save_machine,name='save_machine'),
#your other url paths
]
In your upload.html:
<form action="{% url 'save_machine' %}" method="post">
{% csrf_token %}
<select name="machine_name">
<legend>Select Machine Name</legend>
{% for machine in machines %}
<option value="{{ machine.machine_name }}">
{{ machine.machine_name }}</option><!--Give indentation yourself I can't
because here doesn't have enough
space-->
{% endfor %}
</select>
<br>
<br>
<select name="operation_no">
<legend>Select Operation Number</legend>
{% for machine in machines %}
<option value="{{ machine.operation_no }}">
{{ machine.operation_no }}</option>
{% endfor %}
</select>
<br>
<br>
<br>
<input type="submit" value="Save">
</form>
<tr>
<td>{{choiced_machine.machine_name}}</td>
<td>{{choiced_machine.operation_no}}</td>
</tr>
Fill DropDown list from data in database - Django
You do that with a ForeignKey
[Django-doc], For example:
class Language(models.Model):
name = models.CharField(max_length=50, unique=True)
def __str__(self):
return self.name
class MyModel(models.Model):
language = models.ForeignKey(Language, on_delete=models.PROTECT)
You can then populate the database with records for Language
, and select the language for the MyModel
objects.
If you use a ModelForm
, then standard Django will make a dropdown with the options in the "target" model (and use str(…)
to represent these objects).
It is probably better to set unique=True
[Django-doc] for the name
field, to prevent creating another Language
object with the same name.
By setting on_delete=models.PROTECT
we prevent removing a language, given MyModel
refers with at least one object to that language. So you can only remove languages if no MyModel
is referring to it anymore.
The database will normally guarantee referential integrity. That means that the language
column stores the value of the primary key of the object it refers to. The database normally guarantees that if one such column contains a value x, then there is a primary key with that value in the table for Language
.
Related Topics
How to Determine Whether a Column/Variable Is Numeric or Not in Pandas/Numpy
How to Properly Redirect Stdout/Stderr from a Systemd Service on Raspbian
How to Save a Numpy Array as a 16-Bit Image Using "Normal" (Enthought) Python
How to Run a Function Multiple Times and Return Different Result Python
Find the Last Row from a CSV Input Python
How to Add Thousand Separator to Numbers in Python Pandas Dataframe
Python/Pandas: How to Match List of Strings With a Dataframe Column
Convenient Way to Handle Deeply Nested Dictionary in Python
Running an Excel Macro Via Python
How to Handle Multiple Keys for a Dictionary in Python
Changing File Permission in Python
Plotly: Plot Multiple Figures as Subplots
How to Write to an Existing Excel File Without Overwriting Data (Using Pandas)
Why Calling .Sort() Function on Pandas Series Sorts Its Values In-Place and Returns Nothing
Robot Framework Using Python, Key Press Without Selecting Any Button or Element in the Page