【发布时间】:2016-12-05 03:00:44
【问题描述】:
我无法弄清楚这里发生了什么。我有两个表单,但第二个不会向模板呈现任何输入字段。第二种形式与第一种形式不同。我对编程还是很陌生,但我到处寻找答案,但没有运气。
forms.py
from django import forms
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'})
)
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
self.fields['contact_name'].label = ""
self.fields['contact_email'].label = ""
self.fields['contact_phone'].label = ""
self.fields['content'].label = ""
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(EstimateForm, self).__init__(*args, **kwargs)
self.fields['contact_name'].label = ""
self.fields['contact_email'].label = ""
self.fields['contact_phone'].label = ""
views.py
def contact(request):
form_class = ContactForm
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
messages.success(request, 'Profile details updated.')
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@gmail.com'],
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'
, '')
# Email the profile with the
# contact information
template = get_template('estimate_template.txt')
context = Context({
'contact_name': contact_name,
'contact_email': contact_email,
'contact_phone': contact_phone,
})
content = template.render(context)
send_mail('Email from your website', content, context['contact_email'],
['email@gmail.com'],
fail_silently=False)
return redirect('main/index-v3.html')
return render(request, 'main/index-v3.html', {
'e_form': form_class,
})
模板
<form role="form" action="" method="post" class="contact-form">
{% csrf_token %}
{{ e_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="fsdSFKSDJDKFsdkfjJFKD">
<button type="submit" class="thm-btn">Submit</button>
</form>
【问题讨论】:
标签: django django-forms