将 Django 表单传递给模板标签

Passing a Django form to a template tag

我正在尝试在我的 Wagtail 网站的每一页上的 jQuery 滑出框中呈现一个 Django 表单.我已经创建了一个当前正在呈现框的模板标记,但未显示表单。我在想我错误地传递了表单变量。我的模板标记文件如下所示:

from django import template

from django.shortcuts import render, redirect
from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context

from .forms import DemoForm

register = template.Library()


@register.inclusion_tag('demo_form.html')
def book_a_demo(request):
   form = DemoForm

   if request.method == 'POST':
        demo_form = form(data=request.POST)

        if demo_form.is_valid():
            contact_name = request.POST.get('name', '')
            company_name = request.POST.get('company', '')
            contact_email = request.POST.get('email', '')
            contact_phone = request.POST.get('phone', '')

            # Email the profile with the
            # contact information
            template = get_template('contact_template.txt')
            context = Context({
                'contact_name': contact_name,
                'company_name': company_name,
                'contact_email': contact_email,
                'contact_phone': contact_phone,
            })
            content = template.render(context)

            email = EmailMessage(
                "Request to Book a Demo",
                content,
                "domain" +'',
                ['example@email.com'],
                headers = {'Reply-To': contact_email }
            )
            email.send()
            return render(request, 'demo_form_landing.html')

   return render(request, 'demo_form.html', {'form': form})

demo_form.html 看起来像这样:

{% load static %}

<div id="feedback">
  <div id="feedback-tab">Book a Demo</div>
  <div id="feedback-form" style='display:block;' class="col-xs-4 col-md-4 panel panel-default">
    <form role="form" action="" method="post" class="form panel-body">
      {% csrf_token %}
      {{ form.as_p }}
      <button class="btn btn-primary pull-right" type="submit">Send</button>
    </form>
  </div>
</div>

我的表单如下所示:

from django import forms


class DemoForm(forms.Form):
    name = forms.CharField(initial='Your Name', max_length=100)
    company = forms.CharField(initial='Company Name', max_length=100)
    email = forms.EmailField(initial='Email')
    phone = forms.CharField(initial='Phone Number', max_length=100)

我用来尝试在主 base.html 中呈现它的模板标签是 {% book_a_demo request %}

知道我做错了什么吗?我没有收到任何错误;它只是没有出现。我已经坚持了几个小时,这让我发疯。

使用 inclusion_tag 时,您不应自己调用 render。您应该 return 上下文字典(例如:return {'form': form})- Django 将负责呈现您在 @register.inclusion_tag('demo_form.html') 声明中命名的模板。

我建议不要尝试处理标记内的 if request.method == 'POST': 逻辑,因为只要响应 POST 请求呈现您网站上的任何页面,就会触发该逻辑 - 无论POST 请求是否与您的 DemoForm 有任何关系。相反,您应该设置一个 Django 视图来处理这些表单提交,并将 URL 放入表单的 action 属性中的该视图。

当请求不是 POST 或 is_valid() 为 false 时返回什么?我通常使用这样的模式(注意最后的变化):

from .forms import DemoForm

register = template.Library()

@register.inclusion_tag('demo_form.html')
def book_a_demo(request):
    form = DemoForm

    if request.method == 'POST':
        demo_form = form(data=request.POST)

        if demo_form.is_valid():
            contact_name = request.POST.get('name', '')
            company_name = request.POST.get('company', '')
            contact_email = request.POST.get('email', '')
            contact_phone = request.POST.get('phone', '')

            # Email the profile with the
            # contact information
            template = get_template('contact_template.txt')
            context = Context({
                'contact_name': contact_name,
                'company_name': company_name,
                'contact_email': contact_email,
                'contact_phone': contact_phone,
            })
            content = template.render(context)

            email = EmailMessage(
                "Request to Book a Demo",
                content,
                "domain" +'',
                ['example@email.com'],
                headers = {'Reply-To': contact_email }
            )
            email.send()
            return render(request, 'demo_form_landing.html')
    else:
        demo_form = form()

    return render(request, 'demo_form.html', {'form': demo_form})