title: Django Form - How to create a custom form template with a loop for one field?
body: <p>I'm creating a web app which uses Django's templating engine. Having said that, one of my models is called <code>Order</code>. The <code>Order</code> model, aside from the other models, also uses a model <code>Food</code> - this is a <code>ManyToMany</code> relationship. <code>Food</code> model uses model <code>Category</code> to define food categories (which should contain food categories like Pizza, Burgers, Wine, Beers, etc.) - this is a <code>ManyToMany</code> relationship, as well.</p>
  <p>Now, I'm trying to create a form where the user will be shown a form with separated categories. As of now, all of the foods from the <code>Food</code> model are just shown one by one, one below another one. They are not categorized in the form template. Currently, they are shown by default, as a simple list of foods where user can choose any of them. I want the user to choose any foods they want, but display them categorically (all of the pizzas under the Pizza category, all of the burgers under the Burgers category, etc.).</p>
  <p>My <code>models.py</code> looks like this:</p>
  <pre><code>class Category(models.Model):
      &quot;&quot;&quot;
      Model for food categories
      &quot;&quot;&quot;
  
      name = models.CharField(max_length=100)
      description = models.TextField(max_length=300)
      date_created = models.DateTimeField(auto_now=True)
  
      def __str__(self) -&gt; str:
          return f&quot;{self.name} / {self.description[0 : 50]}...&quot;
  
  class Food(models.Model):
      &quot;&quot;&quot;
      Model for foods
      &quot;&quot;&quot;
  
      name = models.CharField(max_length=150)
      description = models.TextField(max_length=400)
      category = models.ManyToManyField(Category)
      price = models.FloatField()
      date_created = models.DateTimeField(auto_now=True)
  
      def __str__(self) -&gt; str:
          return f&quot;{self.name} / {', '.join(self.category.values_list('name', flat=True))} / {self.price} EUR&quot;
  
  
  class Order(models.Model):
      &quot;&quot;&quot;
      Model for orders
      &quot;&quot;&quot;
  
      order_statuses = [
          (&quot;PR&quot;, &quot;Status 1&quot;),
          (&quot;PS&quot;, &quot;Status 2&quot;),
      ]
      customer_name = models.CharField(max_length=255)
      customer_email = models.EmailField(blank=True)
      ordered_foods = models.ManyToManyField(Food)
      table = ForeignKey(Table, on_delete=models.CASCADE)
      status = models.CharField(choices=order_statuses,
          default=&quot;PR&quot;,
          max_length=2)
      total_price = models.FloatField(editable=False, default=0)
      date_created = models.DateTimeField(auto_now=True)
      date_completed = models.DateTimeField(blank=True,
          default=datetime.datetime.strptime('2099-12-31 00:00:00', '%Y-%m-%d %H:%M:%S'))
  
  
      def __str__(self) -&gt; str:
          return f&quot;{self.id} / {self.customer_name} / {self.table.name} {self.table.number} / STATUS: {self.get_status_display()} / {self.total_price} EUR&quot;
  </code></pre>
  <p>My <code>create_order.html</code> (the template with form, looks like this):</p>
  <pre><code>  &lt;form method=&quot;post&quot;&gt;
      {% csrf_token %}
      {{ form | crispy }}
      &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;Submit&lt;/button&gt;
    &lt;/form&gt;
  </code></pre>
  <p>I'm also using <code>crispy_forms</code> if it makes any difference.</p>
  <p>I'm open to any suggestions on how to make the categorization of the foods in the form template.</p>
  <p>Thank you very much</p>