djt111 djt111 - 6 months ago 10
Python Question

GET from js on django

have html this 3 droplist \ 2 button, js send GET with 3 variables mar, yer, kpp

function validate() {
var mar = document.forms.form_id.m.value;
var yer = document.forms.form_id.y.value;
var kpp = document.forms.form_id.k.value;

return mar, yer, kpp;
}
</script>


<body style="background: transparent; border: none;">
<form action="/search/" method="get" id="form_id" type="submit" onSubmit="return validate();">
<table>
<tr>
<td style="text-align:center">
<br>
<select id="m">
<option value="">Марка</option>
{% for article in articles %}
<option value="{{ article }}">{{ article }}</option>
{% endfor %}
</select>
</td>
<td style="text-align:center">
<br>
<select id="y">
<option value="">Год выпуска</option>
<option value="2016">2016</option>

</select>
</td>
<td style="text-align:center">
<br>
<select id="k">
<option value="">Коробка передач</option>
<option value="mt">Механика</option>

</select>
</td>
</tr>
<tr>
<td colspan="3" style="text-align:center">
<input type="submit" value="Отправить">
<input type="reset" value="Очистить">
</td>
</tr>
</table>
</form>
</body>


and def

def search(request):
if 'mar' in request.GET and request.GET['mar']:
q = request.GET['mar']
list = AutoParsingMan.objects.filter(marka=q)
return render_to_response('Mysitez/seach.html',
{'list': list, 'querty': q})
else:
return HttpResponse('Please submit a search term.')


why not work this 1 variable?(mar) and how create def for 3 variales?(mar, yer, kpp)

model.py

class AutoParsingMan(models.Model):
date_ins = models.DateTimeField()
date_change = models.DateTimeField()
change = models.CharField(max_length=15)
date = models.DateTimeField()
date_task = models.DateField()
marka = models.CharField(max_length=50)
year = models.IntegerField()
kpp = models.CharField(max_length=50)

class Meta:
managed = False
db_table = 'auto_parsing_man'

def __str__(self):
return self.marka, self.year, self.kpp


error it - MultiValueDictKeyError at /search/ "'mar'"

def search(request):
if 'mar' in request.GET and request.GET['mar'] and request.GET['year'] and request.GET['kpp']:
q = request.GET['mar']
y = request.GET['year']
k = request.GET['kpp']
list = AutoParsingMan.objects.filter(marka=q, year=y, kpp=k)
return render_to_response('Mysitez/seach.html',
{'list': list, 'querty': q})
else:
return HttpResponse('Please submit a search term.')

Answer

First of all Selcuk is right, you shall build your question better. Questions shall be as objective as possible and yours is quite vague. Yet, i can see where your issue lies.

You confused JavaScript with HTML Forms. They're two separate specifications which aim to be independent from each other. Before building a JavaScript powered form you should first build the form in pure HTML, and once that works you shall add JavaScript on top of it.

For your issue at hand you used the mar name as a JavaScript variable name, which will never be sent by the HTML form because HTML forms are not aware of JavaScript variables. Instead you shall use the name= parameter on your select tags as follows:

<br>
    <select id="m" name="mar">
        <option value="">Марка</option>
        {% for article in articles %}
            <option value="{{ article }}">{{ article }}</option>
        {% endfor %}
    </select>
</td>
<td style="text-align:center">
<br>
    <select id="y" name="yer">
        <option value="">Год выпуска</option>
        <option value="2016">2016</option>

    </select>
</td>
<td style="text-align:center">
<br>
    <select id="k" name="kpp">
        <option value="">Коробка передач</option>
        <option value="mt">Механика</option>

    </select>
</td>

The HTML form will send the mar variable with the option of whatever article was selected from the dropdown.


Note

That code style might be okay for learning django and/or web development in general but in the real world you shall delegate the construction of the form to django, in a file often called forms.py. Django has a form engine that gives much better compatibility than hardcoding a form in the template.