How to save a <select> option in the database? DJANGO

Asked

Viewed 681 times

2

Hello, I’m a beginner in Django and I’m making simple applications just to train the use of Database with the use of forms. Inside my form, I have a part where the person will select a certain category, however, the selected category is not being saved in my database.

For example: I have inside the models.py file, an object called Idea, done as follows:

class Ideia(models.Model):
    CATEGORIAS = ( 
        ('CAT1', 'Categoria 1'),
        ('CAT2', 'Categoria 2'),
        ('CAT3', 'Categoria 3'),
        ('CAT4', 'Categoria 4'),
        ('OUTROS', 'Outros'),
    )

    pessoa = models.ForeignKey(
        Pessoa, on_delete=None
    )

    titulo = models.CharField(
        max_length=255,
        verbose_name='Nome da ideia'
    )

    descricao = models.TextField(
        verbose_name='Descreva sua ideia'
    )

    categoria = models.CharField(
        verbose_name='Categorias',
        choices=CATEGORIAS,
        max_length=255
    )

My views.py file has the following structure:

def cadastrar_ideia(request):
    if request.method == 'POST':
        email_pessoa = request.POST.get('email')
        pessoa = Pessoa.objects.filter(email=email_pessoa).first()
        if pessoa is not None:
            ideia = Ideia()
            ideia.pessoa = pessoa
            ideia.titulo = request.POST.get('titulo')
            ideia.descricao = request.POST.get('descricao')
            ideia.categoria = request.POST.get('categoria')
            ideia.save()
            print('Deu certo')

            return redirect('/sobre')

    return render(request, 'ideia.html', {})

And finally, my structure in HTML is as follows:

<form action="/ideias" method="post">
    {% csrf_token %}
    <input type="hidden" name="email" value="{{pessoa.email}}">
    <input type="text" name="titulo" placeholder="Nome da ideia">
    <label for="descricao">Descreva sua ideia</label>
    <textarea name="descricao" cols="30" rows="10">
    </textarea>
    <select name="categoria">
        <option value="CAT1">Categoria 1</option>
        <option value="CAT2">Categoria 2</option>
        <option value="CAT3">Categoria 3</option>
        <option value="CAT4">Categoria 4</option>
        <option value="OUTROS">Outros</option>
    </select>
    <input type="submit" value="Cadastrar Ideia">
</form>

All other attributes are being saved in my database, however, in the categories part of an idea registered in the system, just "----" appears, as if nothing was selected. I’m forgetting something important for category information to be saved?

1 answer

1

Use a Createview to resolve this, try the following:

from django.views import generic
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse_lazy


class IdeiaCreate(SuccessMessageMixin, generic.CreateView):

    model = Ideia
    fields = '__all__'
    template_name = 'ideia.html'
    success_url = reverse_lazy('name_de_sua_url')
    success_message = 'Deu certo!'

In your template, change the form to, leave the

<form action="" method="post">
    {% csrf_token %}
    {% for field in form %}
        {{ field.label_tag }}
        {{ field }}
    {% endfor %}
    <input type="submit" value="Cadastrar Ideia">
</form>

To display the success message in the template, just do the following:

{% for m in messages %}
    {{ m }}
{% endfor %}

Browser other questions tagged

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