3

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 %}

私が間違っていることは何か分かりますか?エラーは発生しません。表示されないだけです。私は何時間もこれにこだわっていて、気が狂っています。

4

2 に答える 2

0

リクエストが 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})
于 2016-10-20T20:31:31.913 に答える