In results.html file I want to check if input value from html page and values from models are same. if so then show the matched result. But my code only executes else part.
This is my results.html
<div class="card-body bg-light">
<h5>Your data :</h5>
<p>Your pH value: {{ ph }} pH. </p>
<p>Your moisture value: {{ moist }}</p>
<div style="color: white;">
{% for g in crop_data %}
{% if ph == g.ph and moist == g.moisture %}
<p>{{ g.name }}</p>
<p>{{ g.ph }}</p>
<p>{{ g.moisture }}</p>
{% else %}
<p>Sorry for your inconvience</p>
{% endif %}
{% endfor %}
</div>
</div>
This is my models.py
from django.db import models
class cropInfo(models.Model):
name = models.CharField(max_length=100)
moisture = models.IntegerField()
ph = models.IntegerField()
description = models.TextField()
image = models.ImageField(upload_to='images/')
def __str__(self):
return self.name
This is my views.py
def results(request):
crop_data = cropInfo.objects.all()
context = {
'crop_data':crop_data,
}
ph = request.POST.get('pH')
moist = request.POST.get('moisture')
print(ph)
print(moist)
return render(request, 'results.html', {'ph':ph, 'moist':moist, 'crop_data':crop_data})
My urls and every other thing works fine but i am stuck in comparing those two values and showing the result on web-page.
This is my index.html
<form method="post" id="form" action="{% url 'results' %}">
{% csrf_token %}
<div class="row">
<div class="col-md-4">
<label id="text" for="pH">pH</label>
<input id="label" type="number" id="pH" name="pH" placeholder="Enter pH value" class="form-control" required>
</div>
</div>
<div class="row mt-4">
<div class="col-md-4">
<label id="text" for="moisture">Moisture</label>
<input id="label" type="number" id="moisture" name="moisture" placeholder="Enter moisture level" class="form-control" required>
</div>
</div>
<div class="row mt-4">
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-primary btn-lg" style='float: left;' id="center" >Process</button>
</div>
</div>
</form>