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>
Related
I want to fetch the data from database. I am using Ajax and then I want to define it in views.py to extract the data and display it in view(webpage) using Django. How may I approach in order to bring "be_total" the data from the database and then display the values of "be_total" in the form of bar chart on my webpage?
Can anyone please help me ?
My codes are:
index.html:
<form class="form-row" method="post" >
{% csrf_token %}
<div class="form-group col-md-2" >
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m1">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m2">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m3">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m4">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m5">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<button type="button" onclick="submitData()">Submit</button>
</div>
</form>
````
````
<script type="text/javascript">
function submit(){
// Get answer from the input element
var dt = document.getElementById("m1").value;
var dtt = document.getElementById("m2").value;
var dttt = document.getElementById("m3").value;
var dtttt = document.getElementById("m4").value;
var dttttt = document.getElementById("m5").value;
// add the url over here where you want to submit form .
var url = 'home';
$.ajax({
url: url,
data: {
'm1': dt,
'm2': dtt,
'm3': dttt,
'm4': dtttt,
'm5': dttttt,
},
dataType: 'JSON',
success: function(data){
// show an alert message when form is submitted and it gets a response from the view where result is provided and if url is provided then redirect the user to that url.
alert(data.result);
if (data.url){
window.open(data.url, '_self');
}
}
});
}
</script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
**views.py:**
def home(request):
majors = Major.objects.filter(percentages__isnull=False).distinct().order_by("pk")
if request.method == 'POST':
form = request.POST.get('be_nextyr_total')
line_chart = pygal.Line(width=1500)
line_chart.title = 'Budget Estimation'
context = {
"chart": line_chart.render_data_uri(),
'majors': majors
}
return render(request, "website/index.html" , context )
you have to specify type in your ajax code like this
type:'POST'
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
nam and mob field are userinput fields which i am using for user input and later i am using them for filtering
{% block content %}
<form class="form-signin" action="" method="POST">
{% csrf_token %}
<div class="mb-3">
<input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Nam" autofocus>
</div>
</div>
<div class="mb-3">
<select class="custom-select center-block" name="mob" id="mob" >
<option>{{ customer.sponsor }}</option>
{% for i in sponsor %}
<option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
{% endfor %}
</select>
<div class="invalid-feedback">
Please select a valid Existing Customer.
</div>
</div>
<div class="mb-3">
Search
Urls.py
path('customer_view',views.customer_view,name='customer_view')
Views.py
def customer_view(request):
print(request.method )
name1 = str(request.POST.get('nam'))
print(name1)
mobile1 = str(request.POST.get('mob'))
print(mobile1)
customers_list = customer.objects.filter(
mobile=mobile1) & customer.objects.filter(name=name1)
sponsors = customer.objects.all().distinct('mobile')
ctx = { 'customer': customers_list, 'sponsor': sponsors }
return render(request, 'pages/customer_view.html', ctx)
You use href which does not submit the form. You need a submit button and change your action of your form to your view url. Try this:
{% block content %}
<form class="form-signin" action="{% url 'customer_view' %}" method="POST">
{% csrf_token %}
<div class="mb-3">
<input type="text" name="nam" id="nam" class="form-control-sm center-block" placeholder="Nam" autofocus>
</div>
</div>
<div class="mb-3">
<select class="custom-select center-block" name="mob" id="mob" >
<option>{{ customer.sponsor }}</option>
{% for i in sponsor %}
<option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
{% endfor %}
</select>
<div class="invalid-feedback">
Please select a valid Existing Customer.
</div>
</div>
<div class="mb-3">
<input type="submit" value="Search"/>
.
.
...
In your form, you write a button as:
Search
But this thus means that this is just a link that links to a new page. As a result, the browser will make an (empty) GET request to the given url, and never submit the form.
You can construct a button that submits the form with:
<form class="form-signin" action="{% url 'customer_view' %}" method="post">
<!-- … -->
<button type="submit" class="btn btn-primary btn-sm" role="button">Search</button>
<!-- … -->
</form>
That being said, a search is often done with a GET request, so you might want to change method="get", and obtain the parameters through request.GET.get(..) instead.
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>
In my template i have a multiple-choice-object like this:
<form action="/hmi/give_trend3/" method="get">
<p>
<select name="n" size="3" multiple="multiple">
{% for tag in tags %}
<option>{{ tag.name }}</option><br>
{% endfor %}
</select>
</p>
</form>
and i want to get all the values (of the multiple-choice) in my
views.py:
def give_trend3(request):
v = request.GET['v']
b = request.GET['b']
nn = request.GET['n'] ....
but in the value nn I find only the last value of the choices.
How do I do that?
Try this,
vals = request.GET.getlist("n", '')
Also bind the id to the options in your template,
<select name="n" size="3" multiple="multiple">
{% for tag in tags %}
<option value="{{ tag.id }}">{{ tag.name }}</option><br>
{% endfor %}
</select>