所以它必须是基本的东西,但我无法弄明白。
我正在尝试按照教程
使用django-mptt在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']
和in views.py
def show_tree(request):
return render_to_response("item/tree.html",
{'nodes':Tree.objects.all()},
context_instance=RequestContext(request))
但我无法弄清楚如何将树从模型导入视图
我尝试了不同的组合,比如
from myapp.models import Tree
from myapp.MPTTModel import Tree
等。但它没有工作,没有导入树我收到错误
global name 'Tree' is not defined
回溯:
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
答案 0 :(得分:0)
我已经弄明白了
我几乎没有问题:
必须包含在view.py
中from item.models import Tree
from django.template import RequestContext
另一个问题是@awwester建议的命名错误