2016-01-04 32 views
1

當我在Wagtail的類模型上運行QuerySet時,出現錯誤。在W CMS CMS中未定義錯誤模型名稱

NameError: name 'BlogPage' is not defined 

下面是代碼:

from django.db import models 

from wagtail.wagtailcore.models import Page 
from wagtail.wagtailcore.fields import RichTextField 
from wagtail.wagtailadmin.edit_handlers import FieldPanel 
from wagtail.wagtailsearch import index 


class IndexPage(Page): 
    intro = RichTextField(blank=True) 


    content_panels = Page.content_panels + [ 
     FieldPanel('intro', classname='full'), 
    ] 


class BlogPage(Page): 
    date = models.DateField("Post date") 
    intro = models.CharField(max_length=250) 
    body = RichTextField(blank=True) 

    search_fields = Page.search_fields + (
     index.SearchField('intro'), 
     index.SearchField('body'), 
    ) 

    content_panels = Page.content_panels + [ 
     FieldPanel('date'), 
     FieldPanel('intro'), 
     FieldPanel('body', classname="full") 
    ] 

不過,如果我使用父Page類我得到預​​期的結果運行一個查詢集。

Page.objects.all() 
[<Page: Root>, <Page: Homepage>, <Page: Blog Page Index>, <Page: Post number 1>, <Page: Post number 2>, <Page: Post number 3>] 

什麼是正確的方式來定義從頁面類繼承的BlogPage和其他頁面類?

+0

當你得到'NameError:name'BlogPage'未定義'錯誤時,你運行的是哪行代碼? – gasman

+0

@gasman,當我得到錯誤消息時,我正在運行的代碼行是:BlogPage.objects.all() – Leo

+0

我猜你已經做了'import wagtail',如果你使用的是命令行(你是?);你是否也從myapp.models導入了BlogPage? – FlipperPA

回答

1

在命令行,以後你:

import wagtail 

...你還需要導入BlogPage模型像這樣:

from myapp.models import BlogPage 

祝你好運!