Pick a select value in Django

Asked

Viewed 1,442 times

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.

1 answer

2


From the point of view of good data modeling you should have defined in Models an association between Aparelho and Fabricante (since an appliance is produced by a manufacturer) but the little that is described of the code is not possible to know if it really exists. But I will consider it in the example and consider that Models is more or less like this:

class Aparelho(models.Model):
    nome = models.CharField(max_length=80)

class Aparelho(models.Model):
    nome = models.CharField(max_length=80)
    fabricante = models.ForeignKey(
        Fabricante, on_delete=models.CASCADE, related_name="aparelho_fabricante"
    )

Accordingly, in Views you need to add the list of manufacturers to the scope of variables you send to the template:

# ...
contexto = {
    # aqui o que você já passa para o contexto
    "fabricantes": Fabricante.objects.all()
    }
return render(request, "template.html", contexto)

Hence in the template you can make use:

<select>
{% for fabr in fabricantes %}
    <option value="{{ fabr.id }}">{{ fabr.nome }}</option>
{% endfor %}
</select>

Hence, the function that will receive the data in Views will already have the id correct that you need to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.