我一直在試圖學習W and和使用StreamField
s和塊的複雜性讓我頭疼。遵循W doc文檔和演示應用程序,我沒有任何問題,但是如果沒有外部幫助,超出這一點似乎是無法克服的。以編程方式使用StreamField內容使用頁面get_context函數
這是我正在嘗試(和失敗)實現。
當玩弄博客應用程序時,我試圖擴展它,以便StreamField
允許基於基於語法高亮添加代碼塊。源代碼由自定義的StructBlock
類(CodeBlock
,自然地)管理,並且是BlogPage
的StreamField
正文的一部分。在W admin管理員中,我可以輸入代碼,使用的語言,應用的高亮樣式以及是否顯示行號。這一切都很好,直到我想根據選定的突出顯示樣式爲渲染模板選擇其他樣式表。這裏是頁面模板如何包括樣式表:
{% block extra_css %}
{# This goes in the page <head> section #}
{% if has_code_block %}
{% if code_colorizer %}
<link rel="stylesheet" type="text/css" href="{% static 'css/highlight_{{ code_colorizer }}.css' %}">
{% else %}
<link rel="stylesheet" type="text/css" href="{% static 'css/highlight_default.css' %}">
{% endif %}
{% endif %}
{% endblock %}
的CodeBlock
它的工作的渲染方法(想法感激地發現 - 和無恥地使用 - 在線)。在應用程序流中的這一點上,將高亮樣式添加到頁面上下文爲時已晚,所以我試圖在重寫的def get_context函數中分解頁面主體字段,但沒有成功。我只是無法通過BlogPage
類中的任何類成員獲取底層的JSON文本或訪問數據。
是否有一種方法可以在數據從數據庫中載入數據之前,然後再傳遞給模板之前,將高亮樣式添加到頁面上下文中?
這是我的基本的博客頁面:
class BlogPage(Page):
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
posted_date = models.DateField("Post date")
edited_date = models.DateField("Edited date", null=True, blank=True)
feed_image = models.ForeignKey('wagtailimages.Image', null=True, blank=True, on_delete=models.SET_NULL, related_name='+')
body = StreamField(BlogStreamBlock)
search_fields = Page.search_fields + [
index.SearchField('body')
]
subpage_types = []
parent_page_types = ['BlogIndexPage']
@property
def blog_index(self):
return self.get_ancestors().type(BlogIndexPage).last()
BlogPage.content_panels = [
FieldPanel('title', classname='full title'),
FieldPanel('posted_date'),
FieldPanel('edited_date'),
StreamFieldPanel('body'),
InlinePanel('related_links', label="Related links"),
]
BlogPage.promote_panels = Page.promote_panels + [
ImageChooserPanel('feed_image'),
FieldPanel('tags'),
]
這是我BlogStreamBlock
類的定義:
class BlogStreamBlock(StreamBlock):
subtitle = CharBlock(icon='title', classname='title')
abstract = RichTextBlock(icon='pilcrow')
paragraph = RichTextBlock()
aligned_image = ImageBlock()
source_code = CodeBlock()
quote = QuoteBlock()
最後,這裏是CodeBlock
類:
class CodeBlock(StructBlock):
LANGUAGE_CHOICES = (
('aspx-cs', '.NET ASP/C#'),
('aspx-vb', '.NET ASP/VisualBasic'),
('csharp', '.NET C#'),
('fsharp', '.NET F#'),
('vbnet', '.NET VisualBasic'),
('ng2', 'Angular 2'),
('html+ng2', 'Angular 2 Html'),
('apache', 'Apache Config'),
('arduino', 'Arduino Sketch'),
('asm', 'Assembly'),
('bash', 'Bash Shell'),
('batch', 'Batch CMD File'),
('c', 'C'),
('cpp', 'C++'),
('cmake', 'CMake'),
('coffeescript', 'Coffee Script'),
('css', 'CSS'),
# ... and many, many more ...
('vhdl', 'Vhdl'),
('registry', 'Windows Registry'),
('xml', 'XML'),
('xml+php', 'XML/PHP'),
('xslt', 'XSLT'),
('yaml', 'Yaml'),
)
COLORIZER_CHOICES = (
('abap', 'Abap'),
('algol', 'Algol'),
('algol_nu', 'Algol Nu'),
# ... finish the list with all the highlight styles in the current version of pygments
('vs', 'Vs'),
('xcode', 'Xcode'),
)
language = ChoiceBlock(choices=LANGUAGE_CHOICES, classname='full')
colors = ChoiceBlock(choices=COLORIZER_CHOICES, classname='full')
code = TextBlock()
line_numbers = BooleanBlock(classname='full')
class Meta:
icon = 'code'
def render(self, value, context=None):
src = value['code'].strip('\n');
lang = value['language']
line_nos = value['line_numbers']
lexer = get_lexer_by_name(lang)
formatter = get_formatter_by_name('html', linenos='table' if line_nos else False, cssclass='codehilite', style='default',noclasses=False)
return mark_safe(highlight(src, lexer, formatter))
請您可以提供更多關於''get_context'方法中已經嘗試過但未能實現的細節?訪問'self.body'應該肯定會給你你感興趣的數據。 – gasman
謝謝,我的錯誤是我忘了發佈頁面,所以當我在身體塊上打圈時,新塊不包括在內。 –