Skip to content

Commit 5e746ea

Browse files
committed
Preserving form input
1 parent bd1caf4 commit 5e746ea

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

listings/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def search(request):
6363
'state_choices': state_choices,
6464
'bedroom_choices': bedroom_choices,
6565
'price_choices': price_choices,
66-
'listings': queryset_list
66+
'listings': queryset_list,
67+
'values': request.GET
6768
}
6869

6970
return render(request, 'listings/search.html', context)

templates/listings/search.html

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@
1212
<div class="form-row">
1313
<div class="col-md-4 mb-3">
1414
<label class="sr-only">Keywords</label>
15-
<input type="text" name="keywords" class="form-control" placeholder="Keyword (Pool, Garage, etc)">
15+
<input type="text" name="keywords" class="form-control" placeholder="Keyword (Pool, Garage, etc)" value="{{ values.keywords }}">
1616
</div>
1717

1818
<div class="col-md-4 mb-3">
1919
<label class="sr-only">City</label>
20-
<input type="text" name="city" class="form-control" placeholder="City">
20+
<input type="text" name="city" class="form-control" placeholder="City" value="{{ values.city }}">
2121
</div>
2222

2323
<div class="col-md-4 mb-3">
2424
<label class="sr-only">State</label>
2525
<select name="state" class="form-control">
2626
<option selected="true" disabled="disabled">State (All)</option>
2727
{% for key,value in state_choices.items %}
28-
<option value="{{ key }}">{{ value }}</option>
28+
<option value="{{ key }}"
29+
{% if key == values.state %}
30+
selected
31+
{% endif %}
32+
>{{ value }}</option>
2933
{% endfor %}
3034
</select>
3135
</div>
@@ -37,15 +41,23 @@
3741
<select name="bedrooms" class="form-control">
3842
<option selected="true" disabled="disabled">Bedrooms (Any)</option>
3943
{% for key,value in bedroom_choices.items %}
40-
<option value="{{ key }}">{{ value }}</option>
44+
<option value="{{ key }}"
45+
{% if key == values.bedrooms %}
46+
selected
47+
{% endif %}
48+
>{{ value }}</option>
4149
{% endfor %}
4250
</select>
4351
</div>
4452
<div class="col-md-6 mb-3">
4553
<select name="price" class="form-control">
4654
<option selected="true" disabled="disabled">Max Price (All)</option>
4755
{% for key,value in price_choices.items %}
48-
<option value="{{ key }}">{{ value }}</option>
56+
<option value="{{ key }}"
57+
{% if key == values.price %}
58+
selected
59+
{% endif %}
60+
>{{ value }}</option>
4961
{% endfor %}
5062
</select>
5163
</div>

0 commit comments

Comments
 (0)