g定制FieldPanel

时间:2018-07-13 16:27:55

标签: wagtail wagtail-snippet

例如,Ice cream shop example用于在Wagtail中使用Django形式。我想出了一个解决方案,使访客可以发送他们的建议,然后让编辑者在摘录部分中做出回应。我的问题是我想将提交的信息显示为只读或需要我编写自定义FieldPanel的段落。但是,我无法弄清楚如何编写代码并定义一个CustomFieldPanel来将模型(例如CharFeild)显示为段落元素。下面是我得到沙发的代码:

  @register_snippet
  class IceCreamFlavour(models.Model):
      flavour_name = models.CharField(max_length=255)
      your_name = models.CharField(max_length=255)
      response = models.CharField(max_length=255, blank=True)
panels = [
    MyCustomFieldPanel('your_name'),
    MyCustomFieldPanel('flavour_name'),
    FieldPanel('response')
]
@property
def get_response(self):
    notice = ""
    if self.response in [None, ""]:
        notice = "NEEDS ATTENTION: "
    return notice

def __str__(self):
    return '{}{} - {}'.format(
        self.get_response,
        self.flavour_name,
        self.your_name,
    )

class FlavourSuggestionPage(Page):
    intro = RichTextField(blank=True)
    thankyou_page_title = models.CharField(
        max_length=255, help_text="Title text to use for the 'thank you' page")
# Note that there's nothing here for specifying the actual form fields -
# those are still defined in forms.py. There's no benefit to making these
# editable within the Wagtail admin, since you'd need to make changes to
# the code to make them work anyway.

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

def serve(self, request):
    from .forms import FlavourSuggestionFrom

    if request.method == 'POST':
        form = FlavourSuggestionFrom(request.POST)
        if form.is_valid():
            flavour = form.save()
            return render(request, 'home/thankyou.html', {
                'page': self,
                'flavour': flavour,
            })
    else:
        form = FlavourSuggestionFrom()

    return render(request, 'home/suggest.html', {
        'page': self,
        'form': form,
    })

0 个答案:

没有答案