Django-MPTT using Parent and Child in same form

37 Views Asked by At

My goal is straightforward: I want to enable the selection of a parent category first and then choose its corresponding child categories.

Unfortunately, I am encountering difficulties in implementing this functionality.

My code is

<form>
    <div class="mb-3">
      <label for="main-category" class="form-label">Ana Kategori</label>
      <select class="form-select" id="main-category" name="main_category">
        <option value="">Seçin</option>
        {% recursetree categories %}
          <option value="{{ node.id }}">{{ node.name }}</option>                            
          <div class="mb-3">
            <label for="sub-category" class="form-label"></label>
            {% if not node.is_leaf_node %}
            <select class="form-select" id="children" name="children">
               
              <option value="{{ node.id }}">{{ children }}</option>
              {% endif %}
              {% endrecursetree %} 
      </select>
    </div>     
    </div> 
    <button type="submit" class="btn btn-primary">Gönder</button>
  </form>

current result: enter image description here

0

There are 0 best solutions below