Make the Custom Navbar/Footer in Pro Template work in Multi-Site Installs
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 35/100
研究方向
先檢視 models.py 中自訂的 Navbar 和 Footer 定義,以及 website_tags.py 中的渲染函式,接著檢查現有的 pro 範本測試和文件。定義網站特定結果與現有未指派執行個體應如何運作,加入必要的 migration 和涵蓋率,並在多網站元件能正確渲染且不破壞現有安裝時更新文件。
由索引模型根據 Issue 內容生成。
描述
Is your feature request related to a problem? Please describe.
We receently implemented WagtailCRX, and noticed that wagtail's Navbar and Footer models in the basic template work in multi-site installations, but the custom Navbar and Footer models in the pro template do not (though you could fall back to wagtail's default models with CRX_DISABLE_NAVBAR = False and CRX_DISABLE_FOOTER = False).
Describe the solution you'd like
We implemented the following in our site, which allows us to use custom Navbar(s) and Footer(s) in our multi-site install. Because nulls are allowed, it won't cause errors on existing installs. In existing installs, though, the template tags as implemented below would cause the components to not be rendered (existing model instances would not associated with any site initially). To resolve this, we could:
- Modify the template tags to check for a setting before implementing their site checks so it won't disappear the navbars and footers in existing projects (falling back to current functionality if
CRX_USE_SITE_FIELD=Falsefor example) - Or, just set the template tags to fall back to showing all Navbar(s) and Footer(s) if there are no site-specific results
Would a PR to update the pro template (and add relevant tests & updates to docs) be of interest? Any additional thoughts/recommendations?
This proposal would also resolve #673
models.py
"""
Create or customize your page models here.
"""
from coderedcms.blocks import (
HTML_STREAMBLOCKS,
LAYOUT_STREAMBLOCKS,
BaseBlock,
BaseLinkBlock,
LinkStructValue,
)
from coderedcms.forms import CoderedFormField
from coderedcms.models import (
CoderedArticleIndexPage,
CoderedArticlePage,
CoderedEmail,
CoderedEventIndexPage,
CoderedEventOccurrence,
CoderedEventPage,
CoderedFormPage,
CoderedLocationIndexPage,
CoderedLocationPage,
CoderedWebPage,
)
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail import blocks
from wagtail.admin.panels import FieldPanel
from wagtail.fields import StreamField
from wagtail.snippets.models import register_snippet
# Other models...
@register_snippet
class Navbar(models.Model):
"""Custom navigation bar / menu."""
class Meta:
"""Meta class for Navbar."""
verbose_name = "Navigation Bar"
name = models.CharField(
max_length=255,
)
content = StreamField(
[
("link", NavbarLinkBlock()),
("dropdown", NavbarDropdownBlock()),
],
use_json_field=True,
)
site = models.ForeignKey( # <-- New Field
"wagtailcore.Site",
on_delete=models.CASCADE,
related_name="navbars",
null=True,
blank=True,
)
panels = [
FieldPanel("name"),
FieldPanel("site"), # <-- New panel item
FieldPanel("content"),
]
def __str__(self) -> str:
return self.name
@register_snippet
class Footer(models.Model):
"""Custom footer for bottom of pages on the site."""
class Meta:
"""Meta class for Footer."""
verbose_name = "Footer"
name = models.CharField(
max_length=255,
)
content = StreamField(
LAYOUT_STREAMBLOCKS,
verbose_name="Content",
blank=True,
use_json_field=True,
)
site = models.ForeignKey( # <-- New Field
"wagtailcore.Site",
on_delete=models.CASCADE,
related_name="footers",
null=True,
blank=True,
)
panels = [
FieldPanel("name"),
FieldPanel("site"), # <-- New panel item
FieldPanel("content"),
]
def __str__(self) -> str:
return self.name
Updated template tags render the navbar(s) and footer(s) for the current site:
website_tags.py
"""Custom template tags for the website app."""
from django import template
from wagtail.models import Site
from website.models import Footer, Navbar
register = template.Library()
@register.simple_tag(takes_context=True)
def get_website_navbars(context):
"""Get the navbars for the current site.
Args:
context: The template context which contains the current request
Returns:
QuerySet: Navbar queryset filtered by the current site
"""
try:
# Get the current request from context
request = context['request']
# Get the current site from the request
current_site = Site.find_for_request(request)
# Return navbars associated with the current site
return Navbar.objects.filter(site=current_site)
except (KeyError, AttributeError):
# Fallback to returning all navbars if we can't determine the current site
return Navbar.objects.all()
@register.simple_tag(takes_context=True)
def get_website_footers(context):
"""Get the footers for the current site.
Args:
context: The template context which contains the current request
Returns:
QuerySet: Footer queryset filtered by the current site
"""
try:
# Get the current request from context
request = context['request']
# Get the current site from the request
current_site = Site.find_for_request(request)
# Return footers associated with the current site
return Footer.objects.filter(site=current_site)
except (KeyError, AttributeError):
# Fallback to returning all footers if we can't determine the current site
return Footer.objects.all()
- 主要語言
- Python
- 星號
- 763
- 分支
- 154
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 沒有貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
coderedcorp/coderedcms 的其他 Issue
-
Type: Bug
難度 1/5 1 小時以內 新手友好度 78/100
coderedcorp/coderedcms#714 ·
-
Type: Bug
難度 1/5 1 小時以內 新手友好度 65/100
coderedcorp/coderedcms#711 ·
-
Type: Bug
難度 3/5 1-2 天 新手友好度 45/100
coderedcorp/coderedcms#713 · 1 則留言 ·
-
難度 4/5 3-5 天 新手友好度 55/100
coderedcorp/coderedcms#710 · 4 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 58/100
coderedcorp/coderedcms#709 ·
查看 coderedcorp/coderedcms 的全部 Issue
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 72/100
letsencrypt/cp-cps#353 ·
-
難度 2/5 1-3 小時 新手友好度 68/100
-
難度 2/5 1-3 小時 新手友好度 84/100
PedestrianDynamics/pyFDS-Evac#394 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
DOI-USGS/pywatershed#421 ·
-
難度 2/5 1-3 小時 新手友好度 78/100
python-pillow/Pillow#10087 · 1 則留言 ·
維護者通常 1 天內回覆