samedi 2 avril 2016

Django- send checkbox values to another view

I read all these topics:

How do I get multiple values from checkboxes in Django

How do I get the values of all selected checkboxes in a Django request.POST?

Get the values of multiple checkboxes in django

I implemented a code, but still it doesnt work correctly. Im getting values correctly here:

VIEW:

class IndexView(TemplateView):
    template_name = 'app_student/home.html'

    def post(self, request):
        #this line returns checked values
        print request.POST.getlist('checks[]')
        url = reverse('app_student:implement')
        return HttpResponseRedirect(url)

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data()
        context['groups'] = StudentGroup.objects.all()
        context['years'] = StudyYear.objects.all()

        return context

However i dont know how to send the correct data to front-end and then to the next page(when sumbit button is pressed).

HTML:

    <button class="btn btn-primary "  value="submit" formaction=
"{% url 'app_student:otherWindow' SOMEVARIABLE %}">Sumbit</button>
    ....
    <form class="box box-default" method="post" action="">
                    {% for i in groups %}
                        {% if years.pk == i.years.pk %}
                        <tr>
                        <td> <input type="checkbox" name="checks[]" value='{{ i.id }}' > </td>
                            <td>{{ i.program }}</td>
                            <td>{{ i.course }}</td>
                            <td>{{ i.group }}</td>
                            <td>{{ i.student_count }}</td>
                            <td>{{ i.program.studyLevel }}</td>
                        </tr>
                        {% endif %}
                    {% endfor %}
                    </form>

I think SOMEVARIABLE should be sent from VIEW - like context['groups'] in HTML it is defined groups , and it is the same with 'years'.

So my question is how to do it with this line also:

somevariable = request.POST.getlist('checks[]')

And then use somevariable in HTML.

BIG THANKS GUYS.


SOME CHANGES

def post(self, request):
    mylist =  request.POST.getlist('checks[]')
    url = reverse('app_student:changing',mylist)
    return HttpResponseRedirect(url)

Can i do this? Maybe it is a solution? app_student:changing is another html which gets mylist variable

ERROR:

TypeError at /kruviai/subgroups/implement/
unhashable type: 'list'
Line:
url = reverse('app_student:changing',mylist) 




Aucun commentaire:

Enregistrer un commentaire