L
L
Lolo Penguin2019-12-18 15:13:41
Django
Lolo Penguin, 2019-12-18 15:13:41

How to get value from select(ChoiceField)?

How to take value (value) from select?
the form

class sortirovkaForm(forms.Form):

    prise_spisok = [
        ('no', 'не важно'),
        ('little', 'сначала дешёвые'),
        ('big', 'сначала дорогие'),
    ]
    categori = forms.ChoiceField(label= '', initial= 'цена', choices= prise_spisok)

view
def indexDef(request):

    catalog = shablonModel.objects.filter(proverka = True)


    if request.method == "POST":
        form = sortirovkaForm(request.POST)
        if form.is_valid():
            # меняю значения из формы
            menau = form.save(commit = False)

            <b>if menau.categori == 'little':
                catalog = catalog.order_by('-price_box')</b>

            return HttpResponseRedirect('/rusa/')
        else:
            return Http404('Ошибка =( Попробуйте повторить позже.')
    else:
        form = sortirovkaForm()


    catalog[:18]
        
    return render(request, "user/index.html", {'catalog' : catalog, 'form_prise' : form, })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antonksa, 2019-12-18
@antonksa

If you really want perversions, then

class ФормаСортировки(forms.Form):

    список_цен = [
        ('no', 'не важно'),
        ('little', 'сначала дешёвые'),
        ('big', 'сначала дорогие'),
    ]
    категория = forms.ChoiceField(label= '', initial= 'цена', choices= список_цен)

quite allowed by python syntax.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question