2

I want to have a table with a checkbox on each row with Django as shown in the image. My Django view.py, models.py and HTML file are mentioned below. How this can be done? Is there any built-in function in Django or what?

Table with check box

I have models file as:

class AppsDescription(db.Model):
    aws_response = aws_list_conf_api_call()

    CHOICES = [("yes", "YES"),
           ("no", "NO")]
    OPTIONS = list()

    for apps in aws_response:
        OPTIONS.append(('{name1}'.format(name1=apps.lower()), '{name2}'.format(name2=apps)), )

    name = db.CharField(max_length=256)
    description = db.TextField()
    plan_to_migrate = db.CharField(choices=CHOICES, max_length=256)
    # app_names = MultiSelectField(choices=OPTIONS)

    def __str__(self):
        return self.name

My views.py as

def createapp(request):
    # import ipdb; ipdb.set_trace()
    form = DashboardForm()

    if request.method == "POST":
        form = DashboardForm(request.POST)
        list_of_inputs = request.POST.getlist("inputs")

        if form.is_valid:
            form.save(commit=True)
            return HttpResponseRedirect(reverse("aws:createapp"))

    server = aws_server_list_conf()

    return render(request, "createapp.html", {'server':server, 'form': form})

My html file as

<form method="POST">
      {{form.as_p}}

      <table>
        <tr>
          <th>Select</th>
          <th>Agent ID</th>
          <th>Configuration ID</th>
          <th>Host Name</th>
          <th>OS Name</th>
          <th>OS Version</th>
          <th>Source</th>
          <th>Time of Creation</th>
          <th>Type</th>
        </tr>

        {% for apps in server %}
        <tr>


          <td><input type="checkbox" name="" value=""></td>

          {% for k,v in apps.items %}

          <td>{{ v }}</td>

          {% endfor %}

        </tr>
        {% endfor %}

      </table>




      {% csrf_token %}
      <input type="submit" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" name="" value="submit">
      <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <div class="alert alert-success alert-dismissible">
                <a  class="close" data-dismiss="modal" aria-label="close">&times;</a>
                <strong>Success!</strong> App information stored Successfully.
              </div>
        </div>
      </div>

      </form>

I want to have a table with checkbox on each row with django as shown in the image.

1 Answer 1

5

Your HTML file needs to look like this:

<form method="POST">
      {{form.as_p}}

      <table>
        <tr>
          <th>Select</th>
          <th>Agent ID</th>
          <th>Configuration ID</th>
          <th>Host Name</th>
          <th>OS Name</th>
          <th>OS Version</th>
          <th>Source</th>
          <th>Time of Creation</th>
          <th>Type</th>
        </tr>

        {% for apps in server %}
        <tr>




          {% for k,v in apps.items %}
          <td><input type="checkbox" name="selected_options" value="v.id"></td>
          <td>{{ v }}</td>

          {% endfor %}

        </tr>
        {% endfor %}

      </table>




      {% csrf_token %}
      <input type="submit" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" name="" value="submit">
      <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <div class="alert alert-success alert-dismissible">
                <a  class="close" data-dismiss="modal" aria-label="close">&times;</a>
                <strong>Success!</strong> App information stored Successfully.
              </div>
        </div>
      </div>

      </form>


Sign up to request clarification or add additional context in comments.

1 Comment

Minor alterations to this worked for me. In the html: <td><input type="checkbox" name="selected_options" value="{{ v.id }}"></td> and then in the view: request.POST.getlist("selected_options")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.