Monday, April 16, 2018

Access form data of POST method in table in Django

currently I am working with Django

In template, I combined table and form and thus the code looks like this:


<form role="form" method="post">{% csrf_token %}                  <table class="table" border="0.5px" >                    <thead>                        <tr>                            <th>ID</th>                            <th>NAME</th>                            <th>LOWER_BOUND</th>                            <th>UPPER_BOUND</th>                            <th>GENE_REACTION_RULE</th>                            <th>SUBSYSTEM</th>                        </tr>                    </thead>                    <tbody>                        {% for object in reactioninfo %}                            <tr>                                <td><input type="checkbox" name="checkbox"   id="checkbox" value="{{ object.id }}"><a href="{{ object.get_absolute_url }}">{{    object.id }}</a></td>                                <td>{{ object.name }}</td>                                <td>{{ object.lower_bound }}</td>                                <td>{{ object.upper_bound }}</td>                                <td>{{ object.gene_reaction_rule }}</td>                                <td>{{ object.subsystem }}</td>                            </tr>                        {% endfor %}                    </tbody>                  </table>                <button type="submit" id="generate" name="generate"   value="generate" class="btn btn-default btn-success pull-right">Generate   Graph</button>                </form>


And in views.py, the code looks like this:


class MetaboliteDetail(FormMixin, generic.DetailView):  model = Metabolites  template_name = 'Recon/metabolite_detail.html'    def get_context_data(self, **kwargs):      pk = self.kwargs.get(self.pk_url_kwarg, None)      context = super(MetaboliteDetail, self).get_context_data(**kwargs)      context['reactions'] = Reactionsmeta.objects.all()      context['reactioninfo'] = Reactions.objects.filter(metabolites__contains=pk)      return context    def generate(self, request):        checklist = request.POST.getlist('checkbox')      btnval = request.POST.get('btnDelete')      if checklist and btnval == 'btnDelete':         context = dict(result=checklist)         return render(request, "Recon/combinegraph.html", context)      elif btnval == 'btnDelete':           context = dict(result=checklist)           return render(request, "Recon/combinegraph.html", context)      else:           context = dict(result=checklist)           return render(request, "Recon/combinegraph.html", context)


However, when I finished this and click on the Generate button, the url is actually not changed and the the page appears HTTP405 error.

What is going wrong and how can I fix this?

--
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/296ecfa9-ba1d-462a-ad26-c2d323aa0939%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment