'Django: how to connect customizable url to GET method
I want to implement a search method that takes as filters properties selected by user from front-end side. The URL I want to create would be something like:
urls.py:
urlpatterns = [
path('', views.get_data),
path(r'^find/<property>/<filter>/<value>', views.filter_view)
]
Where
<property> is the value (1-9) selected from:
<div class="dropdown-wrapper" style="padding-left: 0px">
<select class="dropdown" id="select-properties-dropdown">
<option class="property" value="0">Select property</option>
<option class="property" value="1">GID</option>
<option class="property" value="2">Type</option>
<option class="property" value="3">Province</option>
<option class="property" value="4">Street</option>
<option class="property" value="5">House number</option>
<option class="property" value="6">Postcode</option>
<option class="property" value="7">City</option>
<option class="property" value="8">Municipality</option>
</select>
</div>
<filter> is the value from:
<div class="dropdown-wrapper" >
<select class="dropdown">
<option class="property" value="0">Contains</option>
<option class="property" value="1">Equal to</option>
<option class="property" value="2">Ends with</option>
<option class="property" value="3">Starts with</option>
</select>
</div>
and <value> is the taken from the searchbar:
<div class="dropdown-wrapper">
<form action="find" method="GET">
<input class="input searchbar" type="text"
aria-autocomplete="list"
aria-expanded="false"
id="searchBar"
style="height: 28.5px"
>
<button type="submit"><i class="fa fa-search"></i>
</button>
</form>
</div>
Solution 1:[1]
Use django forms. They have widgets that emulate <select>. You can later use the form in django CBVs like FormView and add your own functionality by simply overriding the form_valid() method.
from django import forms
PROPERTY_CHOICES = [
('1', 'GID'),
('2', 'Type'),
('3', 'Province'),
#...
]
FILTER_CHOICES = [
('0', 'contains'),
('1', 'Equal'),
('2', 'Ends'),
('3', 'Starts'),
#...
]
class PropertyForm(forms.Form):
property = forms.CharField(widget=forms.Select(choices=PROPERTY_CHOICES, attrs={'class': 'property'}))
filter = forms.ChoiceField(choices=FILTER_CHOICES) # uses the select widget
value = forms.CharField()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
