我制作了一个基本的Django表单,并将其实现到了HTML中。在我的views.py中,我正在尝试发送电子邮件,即使我无法发送电子邮件,我仍然应该在终端中看到成功的POST提交和电子邮件详细信息。但是,页面只是刷新而没有任何反应。
views.py片段:
def ticket(request, room_name):
form_class = TicketForm
if request.method == 'GET':
form = TicketForm()
else:
form = TicketForm(request.POST)
# validating and cleaning data
if form.is_valid():
type_of_issue = form.cleaned_data['type_of_issue']
#a lot of fields are cleaned, irrelevant to question
try:
send_mail("Issue/Feedback for Room " + room_name, message, from_email, ['admin@example.com'])
except BadHeaderError:
return HttpResponse('Invalid header found')
return redirect('index') #index is my homepage separate from my form/ticket page
return render(request, 'ticket.html', context={'room_name': room_name, 'room': room, 'form': form_class, })
ticket.html(我的表单所在的位置)代码段:
<form id="ticket-border" class="" method="post">
{% csrf_token %}
<!--a lot of form data goes here, irrelevant to question-->
<input type="submit" value="Submit">
</form>
settings.py与发送电子邮件有关的代码段
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'example@gmail.com'
EMAIL_HOST_PASSWORD = 'notmyrealpassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
我在线上看了很多示例和YouTube视频,但是似乎丢失了提交后控制台上什么都没显示的原因。点击提交后,页面只会刷新。它不应该进入我的if == GET语句,但是我通过更改条件并将其发送到其他地方(不是这样做)来检查它是否不是。
新鲜的眼睛能帮助我解决问题吗?
编辑:忘记添加,我也尝试添加
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
或
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
进入我的settings.py,但这没什么区别。该页面仍然刷新,并且控制台或收件箱中没有任何显示。
根据请求添加了票单:
class TicketForm(forms.Form):
type_of_issue = forms.ChoiceField(
choices=ISSUE_CHOICES, widget=forms.RadioSelect(), required=True)
first_name = forms.CharField(required=False, widget=forms.TextInput(attrs={
'class': 'form-control', 'id': 'first-name', 'type': 'text', 'name': 'first-name', 'maxlength': '50', 'placeholder': 'First name'}))
last_name = forms.CharField(required=False, widget=forms.TextInput(attrs={
'class': 'form-control', 'id': 'last-name', 'type': 'text', 'name': 'last-name', 'maxlength': '50', 'placeholder': 'Last name'}))
email_address = forms.EmailField(required=False, widget=forms.EmailInput(
attrs={'class': 'form-control', 'type': 'email', 'id': 'email', 'name': 'email', 'placeholder': 'Email address'}))
feedback_or_further_details = forms.CharField(
required=True, widget=forms.Textarea(attrs={'class': 'form-control', 'rows': '5', 'name': 'additional-details', 'placeholder': 'Please enter additional details or your feedback here.'}))
affiliation = forms.ChoiceField(
choices=AFFILIATION_CHOICES, widget=forms.CheckboxSelectMultiple(), required=True)