Search code examples
pythondjangodjango-mptt

global name from django-mptt is not defined


So it must be something basic but I cant figure it out .

I am trying to use django-mptt just following the tutorial

created in models.py

    @with_author    
class Tree(MPTTModel):
    name = models.CharField(max_length=50, unique=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

    class MPTTMeta:
        order_insertion_by = ['name']

and the in views.py

 def show_tree(request):
     return render_to_response("item/tree.html",
                           {'nodes':Tree.objects.all()},
                           context_instance=RequestContext(request))

But I cant figure out how to import the Tree from model into views

i tried different combinations like

from myapp.models import Tree
from myapp.MPTTModel import Tree

etc. but its is not working and without importing Tree I am getting error

global name 'Tree' is not defined

Traceback:

File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\I812624\dev\mrp\src\item\views.py" in show_tree
  118.         {'nodes':Tree.objects.all()},context_instance=RequestContext(request))

Exception Type: NameError at /item/tree/
Exception Value: global name 'Tree' is not defined

Solution

  • I have figured it out

    I had few problems :

    had to include in view.py

    from item.models import Tree
    from django.template import RequestContext
    

    and another issue was typo in the naming as @awwester suggested