我正在尝试在 jQuery 滑出框中的 Wagtail 网站的每个页面上呈现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 %}
知道我做错了什么吗?我没有收到任何错误;它只是没有出现。我已经坚持了几个小时,这让我发疯了。