я не знаю, что именно это за проблема, но в моем банке крови, когда я ищу место для позитива и нужное место, я получаю результат, но когда я пытаюсь получить положительную группу крови Ab с определенным местоположениемв нем говорится, что конкретного донора для данного местоположения нет, хотя есть данные с этим местоположением
views.py
def detail(request):
blood_type = request.POST['blood_group']
city = str(request.POST['city']).capitalize()
for i in DonarDetail.objects.all():
i.refresh()
i.save()
list2 = DonarDetail.objects.all().filter(
blood_group=blood_type,
city=city,
number_month=-1,
) | DonarDetail.objects.all().filter(
blood_group=blood_type,
city=city,
number_month__gt=2,
)
number = len(list2)
print(number)
if number > 0:
return render(request, 'login/details.html', {'list': list2, 'number': number})
else:
error = 1
return render(request, 'login/check.html', {'error': error})
models.py
class DonarDetail(models.Model):
Male = 'Male'
Female = 'Female'
choice = (('Male', 'Male'),
('Female', 'Female'),
)
choice1 = (('O Positive', 'O Positive'),
('O Negative', 'O Negative'),
('A Positive', 'A Positive'),
('A Negative', 'A Negative'),
('B Positive', 'B Positive'),
('B Negative', 'B Negative'),
('AB Negative', 'AB Negative'),
('AB Positive', 'AB Positive'),
)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20, blank=True)
name = models.CharField(max_length=50, default=' ')
id_no = models.CharField(max_length=20, unique=True)
gender = models.CharField(max_length=20, choices=choice, default='Male')
age = models.IntegerField(validators=[MinValueValidator(18)])
blood_group = models.CharField(max_length=20, choices=choice1, default='O Positive')
email = models.EmailField(blank=True)
street = models.CharField(max_length=20, verbose_name='Street Number')
street1 = models.CharField(max_length=40, verbose_name="Street Name")
street2 = models.CharField(max_length=40, verbose_name='Street Name 2', blank=True)
city = models.CharField(max_length=20, verbose_name="City")
registered_by = models.CharField(max_length=20, default='Admin')
contact_number = models.IntegerField(validators=[MinValueValidator(10)])
last_donated_date = models.CharField(max_length=20, default='0')
number_month = models.IntegerField()
def __str__(self):
return self.id_no
def refresh(self):
now = datetime.datetime.now()
last = self.last_donated_date
if last != "0" or last != 0 and len(last) > 4:
y, m, d = str(last).split('-')
if int(d) == 0:
self.number_month = 0
else:
k = datetime.datetime(int(y), int(m), int(d))
month1 = now.year
month2 = k.year
if now.year == k.year:
if now.month != k.month:
m2 = now.month - k.month
if now.day >= k.day:
self.number_month = m2 + 1
else:
self.number_month = m2
else:
self.number_month = 0
if now.year > k.year:
m2 = 12*(month1 - month2) - k.month + now.month
if now.day >= k.day:
self.number_month = m2 + 1
else:
self.number_month = m2
else:
self.number_month = -1
пытаться получить данные определенной группы крови, т. е. если у вас положительный результат, то все в порядке, но всякий раз, когда я пытался получить данные любой другой группы крови, я просто получал донора для данного местоположения
details.html
{% extends 'users/base.html' %}
{% block title %}results{% endblock %}
{% block search %}class = "active"{% endblock %}
{% block body %}
{% load mathfilters %}
<div class="container">
{% if list %}
<h3>Available donars</h3>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>blood type</th>
<th>gender</th>
<th>age</th>
<th>city</th>
<th>contact number</th>
<th>Last donated</th>
</tr>
</thead>
<tbody>
{% for l in list %}
<tr>
<td>{{ l.first_name }} {{ l.last_name }}</td>
<td>{{ l.blood_group }}</td>
<td>{{ l.gender }}</td>
<td>{{ l.age }}</td>
<td>{{ l.city }}</td>
<td>{{ l.contact_number }}</td>
{% if l.number_month == -1 %}
<td>New donar</td>
{% elif l.number_month > 12 %}
<td>{{ l.number_month|intdiv:12 }} years {{ l.number_month|mod:12 }} months ago </td>
{% else %}
<td>{{ l.number_month }} months ago</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
<p>Results :{{ number }}</p><br>
{% else %}
<div class="panel panel-danger"><div class="panel-heading">No donar available for given location</div></div>
{% endif %}
</div>
{% endblock %}
результат
search.html
{% extends 'users/base.html' %}
{% block title %}search{% endblock %}
{% block search %}class = "active"{% endblock %}
{% block body %}
{% if error %}
<div class="panel panel-danger"><div class="panel-heading">No donar available for given location</div></div>
{% endif %}
<form class="form-horizontal" action="/bloodbank/search/" method="post">
{% csrf_token %}
<div class="form-group">
<label for="bloodgroup" class="control-label col-sm-2">Blood group:</label>
<div class='col-sm-2'>
<select class="form-control" id="bloodgroup" name="blood_group" required>
<option>O Positive</option>
<option>O Negative</option>
<option>A Positive</option>
<option>A Negative</option>
<option>B Positive</option>
<option>B Negative</option>
<option>AB Positive</option>
<option>AB Negative</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="city">City :</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="city" name="city" placeholder="city" >
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="submit"> </label>
<div class="col-sm-2">
<button type="submit" id="submit" class="btn btn-info" value="submit"><span class="glyphicon glyphicon-search"></span> Search</button>
</div>
</div>
</form>
{% endblock %}
search views.py
def search(request):
error = 0
return render(request, 'login/check.html', {'error': error})