Monday, July 25, 2016

Re: Django selected value


Had a similar problem today, other than using django forms, ensure your select box is named, wen the selct box is named, the selected value witll have the name of the select box:
 example:
<select name='myoptions'>
    <option value='val1'> option 1 </option>
    <option value='val2'> option 2</option>
</select>

in django after posting the form:
you cant get the value by.
selected_opt = request.POST['myoptions']

On Saturday, July 23, 2016 at 8:46:56 PM UTC+3, Volodymyr Kirichinets wrote:
How to get value/name of currently selected drop box item?

my models.py :

class Model2(models.Model):
   
.............................................................................
    choice_field
= models.ManyToManyField(to=Model1)
   
....................................................................................

class Model2Form(ModelForm):
   
class Meta:
        model
= Model2
        fields
= ['choice_field']
   
.............................................................................



my views.py:

def myfunc(request):
    form
= Model2Form()
   
if request.method == 'POST':
        form1
= Model2Form(request.POST)
       
if form1.is_valid():
            form1
.save()
       
return ......................................................................................
    myquery
= Model2.objects.values('choice_field').last()

In template.html when I try {{ myquery.choice_field }} - i get {'choice_field': 5}. How can I get a name of currently selected choice_field item and/or value of currently selected choice_field item?



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10069b0b-ee29-457e-92d3-cbf2adc9f564%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment