簡體   English   中英

在Wagtail中獲取另一頁的數據以獲取受密碼保護的版本

[英]Obtaining another page's data for a password-protected version in Wagtail

我是Python和Wagtail的新手。 我正在使用Wagtail網站進行大部分工作,但只遇到了一個問題。

該站點將顯示一組零售(已完成和正在使用)的產品(工廠),以及用於貿易的同一組產品,並顯示僅貿易價。 我有PlantIndexPagePlantPage模型,后者具有各種產品字段和兩個價格字段。

貿易價格信息應使用Wagtail的“頁面隱私”密碼選項進行密碼保護,因此,我認為我需要一個單獨的索引頁面( TradePlantIndexPage ),可以在TradePlantIndexPage管理員中將此頁面隱私設置應用於此索引頁面,以有效地包含其自身的交易公用PlantPage產品頁面的版本。

所以,我有很多PlantPage頁(每個植物產品),以及一個 TradePlantPage使用頁面RoutablePageMixin@route接受任何的蛞蝓PlantPage產品。

# working: /plants/
class PlantIndexPage(Page):
    def get_context(self, request):
        context = super().get_context(request)
        context['plants'] = Page.objects.exact_type(PlantPage).live().all().order_by('title')
        return context

# working, password protected: /trade/
class TradePlantIndexPage(PlantIndexPage):
    template = 'plants/plant_index_page.html'  # for PlantIndexPage and TradePlantIndexPage

    def get_context(self, request):
        context = super().get_context(request)
        context['trade'] = True  # condition in template depends on this to show trade prices
        return context

# working: /plants/<plant_page_slug>
class PlantPage(Page):
    price_retail = models.DecimalField(...)
    price_trade = models.DecimalField(...)
    # ...

# not yet working: /trade/plant/<plant_page_slug>
class TradePlantPage(RoutablePageMixin, PlantPage):
    parent_page_types = ['TradePlantIndexPage']
    subpage_types = []

    template = 'plants/plant_page.html'  # for PlantPage and TradePlantPage

    def get_context(self, request):
        context = super().get_context(request)
        context['trade'] = True  # condition in template depends on this to show trade price
        context['plant'] = Page.objects.page(???).live()  # how to get PlantPage by slug?
        return context

    @route(r'^([-\w]+)/$')
    def trade(self, request, plant_slug):
        self.trade = True
        # how to render TradePlantPage with PlantPage content based on plant_slug?

我將使用TradePlantIndexPage的植物列表鏈接到可使用TradePlantPage的URL( /trade/plant/<plant_page_slug> ),而不是PlantPage/plants/<plant_page_slug> )。

為了使用TradePlantPage顯示特定植物,如何基於trade @routeplant_slug獲取正確的PlantPage實例,並設置上下文,以便使用page變量的PlantPage模板可以工作?

我在代碼中附加了與此相關的注釋/問題。 謝謝。


更新資料

由於加斯曼的善良和易於理解的答案, TradePlantPage的貿易路線現已開始運作。 按照該答案的建議使用plant而不是page ,我將PlantPage的context方法更新為:

    def get_context(self, request):
        context = super().get_context(request)
        context['trade'] = False
        context['plant'] = context['page']
        return context

這是可行的,但我想知道context['plant'] = context['page']是否是在共享模板中使用plant而不是page的次佳方式。 例如,我想它會使用於字段數據的內存增加一倍? 無論如何,對於這個小型網站而言,這可能不是問題,除非有其他聲音,否則我將使用它。

RoutablePageMixin路由方法中,返回HTTP響應的過程與在普通Django視圖函數中的工作方式相同-請參見Django的“視圖和模板”教程 頁面的template屬性和get_context方法不使用,除非您明確地將它們掛鈎(並且為簡單起見,我建議不要這樣做,而直接在該方法中設置template和context變量)。

您的路線方法將需要執行以下操作:

  • 根據plant_slug檢索正確的PlantPage plant_slug ,我們將使用Django的get_object_or_404幫助器
  • 渲染模板plants/plant_page.htmlplants/plant_page.html傳遞上下文變量trade (設置為True), plant (設置為檢索的PlantPage)和page (設置為當前頁面,即TradePlantPage對象)。

這給了我們代碼:

from django.shortcuts import get_object_or_404, render

class TradePlantPage(RoutablePageMixin, PlantPage):
    parent_page_types = ['TradePlantIndexPage']
    subpage_types = []

    @route(r'^([-\w]+)/$')
    def trade(self, request, plant_slug):
        plant = get_object_or_404(PlantPage, slug=plant_slug)

        return render(request, 'plants/plant_page.html', {
            'trade': True,
            'plant': plant,
            'page': self,
        })

注:在這里我假設你plant_page.html模板設置為預期變量plant從兩個調用時PlantPageTradePlantPage -例如,你正在使用{{ plant.title }}而不是{{ page.title }}輸出工廠名稱。 如果不是這種情況,可以通過在上面的代碼中設置'page': plant,來將PlantPage對象作為page傳遞。 但是,我建議您最好給模板訪問當前正在呈現的“真實”頁面對象的權限-這樣您就可以在網站導航中突出顯示正確的項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM