Django 主页中的摇尾反馈表
告诉我如何得到摇尾形式不是在其单独的模板,而是在主页上,因为我不需要贷款和其他页面。我找不到如何在主模型的get_上下文中指定它这与关于的问题/答案非常相似 尽管如此,这里还是有一种实现此解决方案的方法 例子 在您的my_应用程序/models.py中-Django 主页中的摇尾反馈表,django,forms,wagtail,Django,Forms,Wagtail,告诉我如何得到摇尾形式不是在其单独的模板,而是在主页上,因为我不需要贷款和其他页面。我找不到如何在主模型的get_上下文中指定它这与关于的问题/答案非常相似 尽管如此,这里还是有一种实现此解决方案的方法 例子 在您的my_应用程序/models.py中- class HomePage(Page): """A Page model that represents the home page at the root of all pages.""" # must have way
class HomePage(Page):
"""A Page model that represents the home page at the root of all pages."""
# must have way to know WHICH FormPage to use, this makes it user editable
form_page = models.ForeignKey(
'wagtailcore.Page',
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='embedded_form_page',
help_text='Select a Form that will be embedded on this page.')
# ... all other fields
def get_context(self, request, *args, **kwargs):
"""Add a renderable form to the page's context if form_page is set."""
# context = super(HomePage, self).get_context(request, *args, **kwargs) # python 2.7 syntax
context = super().get_context(request, *args, **kwargs)
if self.form_page:
form_page = self.form_page.specific # must get the specific page
# form will be a renderable form as per the dedicated form pages
form = form_page.get_form(page=form_page, user=request.user)
context['form'] = form
return context
content_panels = Page.content_panels + [
PageChooserPanel('form_page', ['base.FormPage']), # Important: ensure only a FormPage model can be selected
#... other fields
]
然后在模板my_app/templates/my_app/home_page.html中
<div>
{% if self.form_page %}
<form action="{% pageurl self.form_page %}" method="POST" role="form">
{% csrf_token %}
{{ form.as_p }} {# form is avaialable in the context #}
<input type="submit">
</form>
{% endif %}
</div>
{%if self.form_page%}
{%csrf_令牌%}
{{form.as#p}{{form在上下文}中可用
{%endif%}
解释
- 首先,我们提供了一种知道要呈现哪个FormPage的方法,我们可以通过使用
获取第一个FormPage来假设,但这是一种不好的做法,可能不可靠。这就是为什么我们在wagtailcore.Page中添加了一个ForeignKey的原因-注意,我们在这里没有链接到FormPage模型FormPage.objects.get()
- 然后我们限制
内的链接,在示例中,我们的PageChooserPanel
模型位于FormPage
应用程序中,因此base
['base.FormPage]
- 然后我们重写
方法,这是我们需要这样做的唯一真正原因,因为它会获取当前的get\u context
和请求
需要使用当前请求FormPage。getForm
- 最后,我们按照文档中的说明更新模板。不同之处在于,我们的表单发布URL实际上是表单页面,而不是当前页面(主页)
- 重要提示:表单实际上发布到表单页面,而不是您的主页,这意味着我们不需要处理任何类型的发布到主页的请求。这是一个简单的解决方案,但它意味着登录页将呈现为表单页的URL
class HomePage(Page):
"""A Page model that represents the home page at the root of all pages."""
# must have way to know WHICH FormPage to use, this makes it user editable
form_page = models.ForeignKey(
'wagtailcore.Page',
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='embedded_form_page',
help_text='Select a Form that will be embedded on this page.')
# ... all other fields
def get_context(self, request, *args, **kwargs):
"""Add a renderable form to the page's context if form_page is set."""
# context = super(HomePage, self).get_context(request, *args, **kwargs) # python 2.7 syntax
context = super().get_context(request, *args, **kwargs)
if self.form_page:
form_page = self.form_page.specific # must get the specific page
# form will be a renderable form as per the dedicated form pages
form = form_page.get_form(page=form_page, user=request.user)
context['form'] = form
return context
content_panels = Page.content_panels + [
PageChooserPanel('form_page', ['base.FormPage']), # Important: ensure only a FormPage model can be selected
#... other fields
]
然后在模板my_app/templates/my_app/home_page.html中
<div>
{% if self.form_page %}
<form action="{% pageurl self.form_page %}" method="POST" role="form">
{% csrf_token %}
{{ form.as_p }} {# form is avaialable in the context #}
<input type="submit">
</form>
{% endif %}
</div>
{%if self.form_page%}
{%csrf_令牌%}
{{form.as#p}{{form在上下文}中可用
{%endif%}
解释
- 首先,我们提供了一种知道要呈现哪个FormPage的方法,我们可以通过使用
获取第一个FormPage来假设,但这是一种不好的做法,可能不可靠。这就是为什么我们在wagtailcore.Page中添加了一个ForeignKey的原因-注意,我们在这里没有链接到FormPage模型FormPage.objects.get()
- 然后我们限制
内的链接,在示例中,我们的PageChooserPanel
模型位于FormPage
应用程序中,因此base
['base.FormPage]
- 然后我们重写
方法,这是我们需要这样做的唯一真正原因,因为它会获取当前的get\u context
和请求
需要使用当前请求FormPage。getForm
- 最后,我们按照文档中的说明更新模板。不同之处在于,我们的表单发布URL实际上是表单页面,而不是当前页面(主页)
- 重要提示:表单实际上发布到表单页面,而不是您的主页,这意味着我们不需要处理任何类型的发布到主页的请求。这是一个简单的解决方案,但它意味着登录页将呈现为表单页的URL