Modified Preorder Tree Traversal

Sarit Ritwirune
Aug 8, 2017 · 2 min read

In short MPTT

Very lucky I founddjango-mptt

Then most likely complete example is here http://kishorkumarmahato.com.np/weekend-exercise-django-mptt-demo/

But unfortunately. His views and forms are brokens.

  1. Form template is totally wrong. You have to use csrf_token and put input in the form tag
  2. get_context_data in ListView is missing the form in the context

Here is my working version.

categories/urls.py

from django.conf.urls import url

from apps.categories.views import CategoryListView, CategoryCreateView

app_name = 'apps.categories'

urlpatterns = [
url(r'^$', CategoryListView.as_view(), name='list'),
url(r'^create/$', CategoryCreateView.as_view(), name='create'),
]

categories/views.py

import datetime

from django.urls import reverse, reverse_lazy
from django.views.generic import CreateView
from django.views.generic.list import ListView

from apps.categories.forms import ProductCategoryForm
from apps.categories.models import ProductCategory
from apps.product_nodes.models import ProductNode


class CategoryListView(ListView):
template_name = 'category_list.html'
model = ProductCategory

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['now'] = datetime.datetime.now()
context['products'] = ProductNode.objects.all()
context['category_list'] = ProductCategory.objects.all()
return context


class CategoryCreateView(CreateView):
model = ProductCategory
template_name = 'category_create.html'
form_class = ProductCategoryForm
success_url = reverse_lazy('category:list')

category_list.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Mtpp Demo | Category List</title>
</head>
<body>
<h1>Category Tree</h1>
{% load mptt_tags %}


<ul>
{% recursetree category_list %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
<h2>Category Tree Select </h2>

<h2>Multiple Select</h2>

<select name="classifiers" multiple="multiple" size="10">
{% for node,structure in category_list|tree_info:"ancestors" %}
{% if node.is_child_node %}
<option value="{{ node.pk }}">
{{ structure.ancestors|tree_path }} :: {{ node }}
</option>
{% endif %}
{% endfor %}
</select>


</body>
</html>

category_create.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create View</title>
<div class="category">
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Go" />
</form>
</div>
</head>
<body>

</body>
</html>

Update 11Sep2017:

https://github.com/heywbj/django-rest-framework-recursive
When you need serializer on recursive field.

Very excited to see it in action.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade