How we can implement CRUD functionality using generic views and django-mptt ?? I’ve searched a lot and couldn’t find a single tutorial/sample code.
Let’s say we have a Course hierarchy or Category hierarchy, or similar thing … How we can Add/Delete/Update/Read them ??
For instance I have this model:
from django.db import models
from mptt.models import MPTTModel , TreeForeignKey
class Genre(MPTTModel):
name = models.CharField(max_length = 50 , unique = True)
parent = TreeForeignKey('self' , null = True , blank = True , related_name = 'children')
class MPTTMeta:
order_insertion_by = ['name']
and this views.py:
from django.views.generic.list_detail import object_list
from mp.models import Genre
def genres_list(request):
''' Shows all of the genres '''
return object_list(request,
queryset = Genre.tree.all() ,
template_name = 'genres.html' ,
# template_object_name = 'nodes' ## Adding "nodes" variable didn't solve the problem
)
well … I get this error (error is in line number “5” : {% recursetree nodes %}):
Caught VariableDoesNotExist while rendering: Failed lookup for key [nodes] in u"[{'paginator': None, 'is_paginated': False, 'page_obj': None, 'nodes_list': [<Genre: Genre object>, <Genre: Genre object>, <Genre: Genre object>, <Genre: Genre object>]}, {'csrf_token': <django.utils.functional.__proxy__ object at 0x7f5bb810f090>}, {'perms': <django.utils.functional.__proxy__ object at 0x7f5bb810ff10>, 'messages': <django.contrib.messages.storage.user_messages.LegacyFallbackStorage object at 0x324af50>, 'user': ....................................
<html>
2
3 {% load mptt_tags %}
4 <ul>
5 {% recursetree nodes %}
6 <li>
7 {{node.name}}
Simple CRUD application with MPTT models and class-based generic views (Django 1.4 The function-based implementation has been deprecated).
Let’s begin
urls.py
models.py
templates/genre_detail.html
templates/genre_form.html
templates/genre_list.html
and that’s it.
I had some spare time today and shared this project on github https://github.com/kaygorodov/simple-crud-mptt.
How can I define my own class-based view?
genre/views.py
genre/urls.py