Access form data of POST method in table in Django

27 views
Skip to first unread message

shaw...@gmail.com

unread,
Apr 16, 2018, 12:18:28 PM4/16/18
to Django users

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?

Reply all
Reply to author
Forward
0 new messages