我已经被这个问题困扰了几天了。我已经搜索了互联网,甚至搜索了stackoverflow,但找不到解决方案,我将不胜感激。我对编程非常陌生,并尝试创建联系表,以便用户可以使用联系表将电子邮件发送给注册用户。当我在浏览器中打开页面时,仅显示“发送您的消息”和“提交”按钮。实际的形式没有。
我有以下代码:PS,我导入了所有必需的模块。
forms.py:
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True)
content = forms.CharField(
required=True,
widget=forms.Textarea
)
views.py:
def profile_contact(request):
if request.method == 'POST':
pc_form = ContactForm(request.POST)
if pc_form.is_valid():
sender_name = pc_form.cleaned_data['contact_name']
sender_email = pc_form.cleaned_data['contact_email']
message = "{0} has sent you a new message:\n\n{1}".format(sender_name, pc_form.cleaned_data['content'])
send_mail('New Enquiry', message, sender_email, ['enquiry@exampleco.com'])
return redirect('home')
else:
pc_form = ContactForm()
return render(request, 'contact_profile.html', {'form': pc_form})
urls.py:
path('profile_contact/', user_view.profile_contact, name='profile_contact'),
contact_profile.html:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form action="" method="POST">
<fieldset class="form-group">
<legend class="border-bottom mb-4">Send your message</legend>
{% csrf_token %}
{{ pc_form | crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info"
type="submit">Submit</button>
</div>
</form>
</div>
{% endblock %}
谢谢您的帮助。
答案 0 :(得分:0)
在呈现页面时,在views.py中,您使用{'form':pc_form};而在html中,您将表单称为{{pc_form |脆皮}}。将“表单”更改为“ pc_form”。