1
The HTML code:
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1 align="center">Produtos</h1>
{% block content %}
<form action = "{% url 'produtos' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<legend class="lead">PRODUTOS </legend>
<select name="m" class="form-control">
{% for marcas in m %}
<option value="{{marcas.codigo_id}}">{{ marcas }}</option>
{% endfor %}
</select>
<select name="anome" id="anome">
{% for aparelhos in a %}
<option value="aparelhos">{{ aparelhos }}</option>
{% endfor %}
</select>
<select name="pnome" id="pnome">
{% for produtos in p %}
<option value="produtos">{{ produtos }}</option>
{% endfor %}
</select>
<input type = 'submit' value="Submit">
</div>
</form>
{% endblock content %}
</body>
</html>
The Django code (Views)
from django.shortcuts import render
from .models import Marcas, Aparelhos,
Produtos
def marcas(request):
mnome = request.POST.get("mnome", None)
anome = request.POST.get("anome", None)
if request.method == 'POST':
import pdb
pdb.set_trace()
if mnome:
m = Marcas.objects.all()
if m == 'Apple':
anome = a
a = Aparelhos.objects.filter(marca_id=1)
else:
anome = a
a = Aparelhos.objects.filter(marca_id=1)
return render(request, 'produtos.html',{'a': a, 'm': m })
So how do I capture a selected value in my select(html) and use it in my views.
Thank you very much!!
– Murilo Krugner