I am using a database "selectCountry" and I want to select all the country names in the database to get populated in the html dropdown menu.
Following is my code
models.py
class selectCountry(models.Model):
selectCountryoption = models.CharField(max_length = 30)
views.py
def loadCountries(request):
countries = selectCountry.objects.all()
return render(request, 'register/bg-pages.html', {'countries':
countries})
html code
<form action=".">
<select name="Country" id="searchOption">
<option value="">---------</option>
{% for country in countries %}
<option value="{{ countries.selectCountryoption }}">{{
countries.selectCountryoption }}</option>
{% endfor %}
</select>
<input type="submit" value="submit" name="submit"
</form>
</p>
The problem is the values are not getting populated in the list when I am requesting the page.
You are using the wrong value in your for loop.
<option value="{{ countries.selectCountryoption }}">{{
countries.selectCountryoption }}</option>
should be
<option value="{{ country.selectCountryoption }}">{{
country.selectCountryoption }}</option>
Related
Just started learning Django where I faced this problem
I want to create eCommerce site where users can select their location(Like State & Area) and accordingly the products will be displayed to do this I have already created two drop downs in the navbar.html which is extended by the base.html which is extended by the home.html.
cart_template_tags.py:
register = template.Library()
#register.filter
def load_city(request):
locations = Locations.objects.filter()
return locations
#register.filter
def load_areas(request):
locations = Area.objects.filter()
return locations
navbar.html:
{% load cart_template_tags %}
<form method="GET" action=".">
<ul class="navbar-nav nav-flex-icons">
<select id="city" class="form-control" name="city">
<option selected>city...</option>
{% for cityVal in request.user|load_city %}
<option value="{{ cityVal }}">{{ cityVal }}</option>
{% endfor %}
</select>
<select id="area" class="form-control" name="area">
<option selected>area...</option>
{% for areaVal in request.user|load_areas %}
<option value="{{ areaVal }}">{{ areaVal }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-primary">Search</button>
</form>
I have displayed products using home view where I rendered home.html
Views.py
class HomeView(ListView):
model = Item
paginate_by = 10
template_name = "home.html"
how can I get the value which I have selected in drop down "city" & "area" in HomeView ?
If you have any suggestion please suggest or if you know any opensource code where this functionality (Location wise product display) is developed you can suggest that too
i have table in db in which i stores countries and i dynamically pass those countries in template and and in forms.pt i have a ChoiceField(widget=forms.Select,required=True) like this but i didnot get the value from this filed but when i chane it into CharField i get the value.
<div class="form-group col-md-4 select-border">
<label>Country</label>
<select class="form-control basic-select" name="country">
{% for country in countries %}
<option value="{{ country.value }}">{{ country.value }}</option>
{% endfor %}
</select>
</div>
form.py
country = forms.ChoiceField(widget=forms.Select,required=True)
views.py
users_meta.candidate_country = ProfileForm.cleaned_data['country']
I have a drop down and it is populated through my models. I am able to select one and then push submit. But the data that I am getting is being broke by spaces in the name. So if I have an option in my drop down menu such as:
Please Pick Me
I will only get
Please
template.html
<form action="{% url 'parsed' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="sel1">Select Test:</label>
<select class="form-control" name="selectedtest" id="sel1">
{% for test in test %}
<option value={{ test.name }}>{{ test.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label>Paste Event JSON</label>
<textarea class="form-control" name="jsontextarea" rows="20"></textarea>
<div style="text-align:center">
</br>
<input class="btn btn-primary" type="submit" value="Parse">
</div>
</div>
</form>
views.py
def parsed(request):
data = request.POST.get('jsontextarea')
testname = request.POST.get('selectedtest')
print(testname)
context = {
"json" : data,
"test" : Test.objects.all(),
"event" : Event.objects.all(),
"platform" : Platform.objects.all(),
"device" : Device.objects.all(),
"property" : Property.objects.all(),
"testname" : testname
}
return render(request, 'jsonparser/parsed.html', context)
Try replacing
<option value={{ test.name }}>{{ test.name }}</option>
with
<option value="{{ test.name }}">{{ test.name }}</option> (Notice the double quotes)
My guess is that your HTML becomes - <option value=Please Pick Me>Please pick me</option> and in this case the value of the option is "Please". If you use double quotes however, it would become <option value="Please Pick Me">Please pick me</option> which is what you want
Hi I have a dropdown like this
<select name="category" data-placeholder="select Category here" multiple
class="chosen-select" tabindex="8" required>
<option value=""></option>
<option>Transport</option>
<option>Accommodation</option>
<option>Ware House</option>
<option>Readymade</option>
</select>
And I am getting selected element of this dropdown from database Filter query like this
categories=Categories.objects.filter(vendor=uid)
when I for loop like this
{% for category in categories %}
<option value=""></option>
<option value="{{ category.category }}"{% if category.category == 'Transport' %}selected{% endif %}>Transport</option>
<option value="{{ category.category }}"{% if category.category == 'Accommodation' %}selected{% endif %}>Accommodation</option>
<option value="{{ category.category }}"{% if category.category == 'Activity' %}selected{% endif %} >Activity</option>
<option value="{{ category.category }}"{% if category.category == 'Readymade' %}selected{% endif %}>Pre Packaged Plan</option>
</option>
{% endfor %}
In this case For example If I have 2 options selected in database then it prints Options two time but seleted result is correct. Any help would be highly appreciated thanks.
If categories is a list of the categories you want to be selected, then you can make that a list (category_names = [category.category for category in categories]), and in your HTML, do not iterate over the categories (that would result in N times the categories), but rather check if each option was in the selected list:
<select ...>
<option value=""></option>
<option value="Transport" {% if 'Transport' in category_names %}selected{% endif %}>Transport</option>
<option value="Accommodation" {% if 'Accommodation' in category_names %}selected{% endif %}>Accommodation</option>
...etc
</select>
Now, if you need to populate this <select> with category names dynamically, that's another issue but it's not how I interpreted this question.
I am having problem to get the selected data from a form. Here is my form
<form action="#" method="GET">
{% csrf_token %}
<select name="country" id="selectcountries" class="custom-select">
<option>Select country</option>
{% for item in countries %}
<option val="{{ item.name }}"> {{ item.name }} </option>
{% endfor %}
</select>
<select name ="city" id="selectcities" class="custom-select">
<option>Select city</option>
</select>
<select class="custom-select" name="option" >
<option selected> Tourist Spot </option>
<option> Hotel </option>
<option> Restaurent </option>
</select>
<button type="submit" class="btn tour-btn"><i class="fa fa-search pr-2" aria-hidden="true"></i> Search </button>
</form>
And my views.py is
def advanceSearch(request):
country = request.GET.get('country')
city = request.GET.get('city')
option = request.GET.get('option')
if request.method == "GET" :
if country:
message = 'q= %s' % country
else:
message = 'Empty'
else:
message = 'oops'
return HttpResponse(message)
HTTPResponse always give me empty message even after with passing values by the form. I want to get the data from this form but i cant.
I tried to replicate the scenario with the provided code, and I think your search view is not getting executed. You have provided {% url 'advanceSearch' %} in the anchor tag inside button. It should be in the action attribute of the form.
<form action="{% url 'advanceSearch' %}" method="GET">
{% csrf_token %}
<select name="country" id="selectcountries" class="custom-select">
<option>Select country</option>
{% for item in countries %}
<option val="{{ item.name }}"> {{ item.name }} </option>
{% endfor %}
</select>
<select name ="city" id="selectcities" class="custom-select">
<option>Select city</option>
</select>
<select class="custom-select" name="option" >
<option selected> Tourist Spot </option>
<option> Hotel </option>
<option> Restaurent </option>
</select>
<button type="submit" class="btn tour-btn"><i class="fa fa-search pr-2" aria-hidden="true"></i>Search</button>
</form>