Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8251859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:22:30+00:00 2026-06-08T00:22:30+00:00

How we can implement CRUD functionality using generic views and django-mptt ?? I’ve searched

  • 0

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}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:22:31+00:00Added an answer on June 8, 2026 at 12:22 am

    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

    from django.conf.urls.defaults import patterns, include, url
    from django.views.generic import DetailView, ListView, CreateView, UpdateView
    from genre.models import Genre
    
    urlpatterns = patterns('',
        url(r'detail/(?P<pk>\d+)', DetailView.as_view(model=Genre), name="genre_detail",),
        url(r'update/(?P<pk>\d+)', UpdateView.as_view(model=Genre), name="genre_update",),
        url(r'create', CreateView.as_view(model=Genre), name="genre_create",),
        url(r'list', ListView.as_view(model=Genre), name="genre_list",),
    )
    

    models.py

    from django.core.urlresolvers import reverse
    from django.db import models
    from mptt.models import MPTTModel
    
    
    class Genre(MPTTModel):
        name = models.CharField(max_length=50 , unique=True)
        parent = models.ForeignKey('self' , null=True , blank=True , related_name='children')
    
        def get_absolute_url(self):
            return reverse('genre_detail', kwargs={'pk': self.pk, })
    
        class MPTTMeta:
            order_insertion_by = ['name']
    

    templates/genre_detail.html

    <html>
    <body>
    
    <div>Object: {{ object }}</div>
    <div>Object's name: {{ object.name }}</div>
    <div>Object's parent: {{ object.parent }}</div>
    
    </body>
    </html>
    

    templates/genre_form.html

    <html>
    <body>
    
    <form action="" method="post">
    {% csrf_token %}
    {{ form.as_ul }}
    
    <button>save</button>
    </form>
    </body>
    </html>
    

    templates/genre_list.html

    {% load mptt_tags %}
    <html>
    <body>
    <ul class="root">
        {% recursetree object_list %}
            <li>
                {{ node.name }}
                {% if not node.is_leaf_node %}
                    <ul class="children">
                        {{ children }}
                    </ul>
                {% endif %}
            </li>
        {% endrecursetree %}
    </ul>
    </body>
    </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

    from django.view.generic import UpdateView
    class MyCustomUpdateView(UpdateView):
        model = Genre
    
        def get_form_kwargs(self):
            """
            Returns the keyword arguments for instanciating the form.
            """
            kwargs = super(MyCustomUpdateView, self).get_form_kwargs()
            kwargs.update({'my_first_param_to_init_form': 1,
                          'my_second_param_to_init_form': 2,
            })
            return kwargs
    

    genre/urls.py

    url(r'update/(?P<pk>\d+)', MyCustomUpdateView.as_view(), name="genre_update",),
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can implement reordring in winforms datagridview by using the feature of drag and
I know how can implement listview without using listactivity.But i dont know how get
Can somebody explain how I can implement semaphore in Objective-C? I did a lot
I alreay can implement chat daemons using gevent and zeromq, but I'd like to
I can implement both variants - it's easy. But I'm interested: what approach is
Can someone please tell me how I can implement the following line of pseudo-code.
Anyone knows how i can implement re-arrangable divs (Drag the divs around on the
How I can implement dynamic Jtree, which shows created instances of clases? For example,
I know that classes can implement various special methods, such as __iter__ , __setitem__
How I can implement Background Processing in asp.net. Can anyone give a small and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.