gpt4 book ai didi

python - :Django forms - input boxes not rendering in template

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:19 25 4
gpt4 key购买 nike

我无法让我的表单显示在渲染的模板上。提交按钮在那里,但没有其他东西。我有两种表单,一种工作正常,它的 EstimateForm 我无法显示。

编辑:我忘了提及估算表单是基本模板的一部分,特别是它位于网站页脚中。联系模板是基本模板的扩展。基本模板中的表单未显示在任何模板上。很抱歉一开始没有添加此信息。

这是模板

<form role="form" action="" method="post" class="contact-form" style="margin-top: 25px">
{% csrf_token %}
{{ estimate_form.as_p }}
<button type="submit" class="thm-btn">Submit</button>
</form>

这就是渲染的内容

<form role="form" action="" method="post" class="contact-form" style="margin-top: 25px" novalidate="novalidate">
<input type="hidden" name="csrfmiddlewaretoken" value="lfudMn6U8TBJ2czhZM4UZTINnP6xoLZs">

<button type="submit" class="thm-btn">Submit</button>
</form>

表格。 py

class ContactForm(forms.Form):
contact_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Name*'}))
contact_email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Email*'}))
contact_phone = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Phone Number*'}))
content = forms.CharField(
required=True,
widget=forms.Textarea(attrs={'placeholder': 'Your comments'})
)


class EstimateForm(forms.Form):
contact_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Name*'}))
contact_email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Email*'}))
contact_phone = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Phone Number*'}))

def __init__(self, *args, **kwargs):
super(BottomForm, self).__init__(*args, **kwargs)
self.fields['contact_name'].label = ""
self.fields['contact_email'].label = ""
self.fields['contact_phone'].label = ""

View .py

def contact(request):
form_class = ContactForm

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

if form.is_valid():
contact_name = request.POST.get(
'contact_name'
, '')
contact_email = request.POST.get(
'contact_email'
, '')
contact_phone = request.POST.get(
'contact_phone'
, '')
form_content = request.POST.get('content', '')

# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = Context({
'contact_name': contact_name,
'contact_email': contact_email,
'contact_phone': contact_phone,
'form_content': form_content,
})
content = template.render(context)
send_mail('Email from your website', content, context['contact_email'],
['email'],
fail_silently=False)
return redirect('/contact')
return render(request, 'main/contact.html', {
'form': form_class,
})


def estimate(request):
form_class = EstimateForm

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

if form.is_valid():
contact_name = request.POST.get(
'contact_name'
, '')
contact_email = request.POST.get(
'contact_email'
, '')
contact_phone = request.POST.get(
'contact_phone'
, '')
form_content = request.POST.get('content', '')

# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = Context({
'contact_name': contact_name,
'contact_email': contact_email,
'contact_phone': contact_phone,
'form_content': form_content,
})
content = template.render(context)
send_mail('Email from your website', content, context['contact_email'],
['@gmail.com'],
fail_silently=False)
return redirect('/contact')
return render(request, 'main/contact.html', {
'estimate': form_class,
})

最佳答案

您正在传递带有“估计”键的表单

return render(request, 'main/contact.html', {
'estimate': form_class,
})

但是在模板中您试图通过

访问它
{{ estimate_form.as_p }}

只需将其更正为{{estimate.as_p }}

关于python - :Django forms - input boxes not rendering in template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40578257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com