bland-coat-6833
12/03/2021, 6:51 PMfast-baker-86727
12/03/2021, 7:31 PMnice-action-92152
12/05/2021, 3:21 AMhtml
**dog_list.html** -- page you visit
<!DOCTYPE html>
<html>
{% extends 'main/header.html' %}
{% load static %}
{% block content %}
<section class="col-4 offset-1">
{% include 'main/search-males.html' %}
</section>
{% endblock %}
</html>
html
**search-males.html** --performs the search with view "search_dogs"
<script src="https://unpkg.com/htmx.org@1.6.1"></script>
<div class="d-flex justify-content-end mb-4">
<form>
{% csrf_token %}
<input type="text"
hx-post="{% url 'main:search_dogs' %}"
hx-target='#males'
hx-trigger="keyup changed delay:500ms"
name="searchmales"
class="form-control-sm mr-2"
placeholder="Search Males..." />
</form>
</div>
<div id="males"></div>
html
**search_results.html** --displays the results of the search. This is where I would need help.
{% if maledogs %}
<div class="container">
<ul class="list-group col-xs-12">
{% for male in maledogs %}
{% if male.sex == "male" %}
<li><a href="#!" class="blue-text">{{male.registered_name}}</a></li>
{% endif %}
{% endfor %}
</ul>
</div>
{% endif %}
Image of the result:nice-action-92152
12/05/2021, 3:23 AMnice-action-92152
12/05/2021, 3:24 AMlimited-diamond-93357
12/05/2021, 11:46 AMlimited-diamond-93357
12/05/2021, 11:53 AMlimited-diamond-93357
12/05/2021, 11:53 AMnice-action-92152
12/05/2021, 8:44 PMnice-action-92152
12/05/2021, 8:45 PMlimited-diamond-93357
12/06/2021, 1:49 AMmain:search_dogs
should be filtering on the value of searchmales
and returning just the results from that.limited-diamond-93357
12/06/2021, 1:51 AMmaledogs
all the dogs from the database, or the result of filtering by searchmales
in your view?limited-diamond-93357
12/06/2021, 1:57 AMif
in your template if I wanted conditional behaviour for example, adding a class to the HTML for some some rows. But for what you have there (all or nothing), you're better off doing this in the view, so it's done at db level. Something like:
def search_dogs(request):
โฎ
male_name_filter_str = request.POST['searchmales']
maledogs = Dogs.objects.filter(registered_name__startswith=male_name_filter_str, sex='male')
return render(response, 'main/search-males.html', {'maledogs', maledogs})
nice-action-92152
12/06/2021, 3:07 AMsearchmales
is indeed the result of a very similar query occurring in the view. With nearly the same render returned.
python
query_males = request.POST.get('searchmales')
if query_males:
maledogs = Dogs.objects.filter(Q(call_name__icontains=query_males) | Q(registered_name__icontains=query_males) | Q(owner_name__icontains=query_males))
That thing works fantastic with the htmx. handling the query dynamically. Smooth, fast. My example of loading the whole db is just because I can't figure out how to do the auto-complete with materializecss and jquery, so I send it the whole database. This is the HTML for that example (condensed to the main parts). In this case, dogs
is my entire db.
html
<form method="GET" action="{% url 'main:test_complete' %}">
<div class="input-field col s12">
<i class="material-icons prefix">search</i>
<input name="sireid" value="{{request.GET.sireid}}" type="text" id="sireid" class="sireid">
<label for="sireid">Enter Male Registered Name</label>
</div>
</form>
<script>
var sires = {
{% for dog in dogs %}
{% if dog.sex == "male" %}
"{{ dog.registered_name }}":null,
{% endif %}
{% endfor %}
}
$(document).ready(function(){
$('input.sireid').autocomplete({
data: sires
});
});
</script>
I have tried copying the above and switching it up a bit to use the value of searchmales
and I can't seem to get it to function correctly.nice-action-92152
12/06/2021, 6:37 AMselect
built off of a active search
prehistoric-cat-63987
12/07/2021, 6:54 AMbland-coat-6833
12/07/2021, 8:29 AMbland-coat-6833
12/07/2021, 8:30 AMworried-nest-35606
12/07/2021, 10:42 AMworried-nest-35606
12/07/2021, 11:01 AMbitter-dog-98292
12/07/2021, 11:34 AMbland-coat-6833
12/07/2021, 11:53 AMbland-coat-6833
12/07/2021, 12:52 PMworried-nest-35606
12/07/2021, 1:16 PMbitter-dog-98292
12/07/2021, 3:08 PMfast-baker-86727
12/08/2021, 12:32 AM<tr hx-target="this" hx-swap="outerHTML">
<form action="" method="POST" hx-post="ingredients/htmx/edit/{{ ingredient.pk }}">
{% csrf_token %}
<td>{{ form.name }}</td>
<td>{{ form.quantity }} {{ form.unit }} (s)</td>
<td>${{ form.price_per_unit }} / {{ ingredient.unit }} (s)</td>
<td>
<div class="btn-group">
<input type="submit" value="Submit" class="btn btn-success" />
<button class="btn btn-danger" hx-get="/ingredients">Cancel</button>
</div>
</td>
</form>
</tr>
hundreds-camera-24900
12/08/2021, 12:38 AMfast-baker-86727
12/08/2021, 12:42 AMhundreds-camera-24900
12/08/2021, 12:43 AMhundreds-camera-24900
12/08/2021, 12:44 AM