【发布时间】:2015-02-13 15:23:39
【问题描述】:
我是 Django 新手。我创建了一个包含姓名、电子邮件、电话、消息的表单。当我将其用作 html 文件的一部分时,该表单可以正常工作。 html中表单的代码,可以正常工作:
<html>
<head></head>
<body>
<!--Message Form here-->
<form method='POST' action='/support/'> {% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-success" value="Send Message" />
</form>
<!--Message Form ends here-->
</body>
</html>
当我将此 html 扩展为基类,然后将引导 css 添加到 html 时,提交按钮不起作用,因此无法正确提交表单(稍后提供代码)。
视图代码:
def support(request):
if request.method == 'POST':
from django.core.mail import send_mail
form = SupportMessageForm(request.POST or None)
if form.is_valid():
name = form.cleaned_data['name']
sender = form.cleaned_data['email']
telephone = form.cleaned_data['telephone']
subject = "Support Form - Message"
message = "From: " + name + " ( " + sender + " ) " + "Phone: " + telephone + "\n\n"
message += form.cleaned_data['message']
recipients = ['#SECRET#']
save_it = form.save(commit=False)
save_it.save()
result = "Your message has been delivered. Thank you for contacting us! We will get in touch very soon.."
try:
send_mail(subject, message, sender, recipients)
except smtplib.SMTPException as e:
result = str(e)
return render(request, "support.html", {"result": result, "style": "display: block", 'form': form})
else:
return render(request, "support.html", {"result": "Failed to send the message. Please validate your data. ",
"style": "display: block", 'form': form})
else:
form = SupportMessageForm()
return render_to_response('support.html', {'form': form}, context_instance=RequestContext(request))
当我将上述表单代码放入扩展的 html 文件中时,例如:
{% extends "base.html" %}
{% block title %}Support{% endblock %}
{% load staticfiles %}
{% block body_block %}
<section>
<!-- Page Content -->
<div class="container">
<!-- Page Heading/Breadcrumbs -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Our Support
<small>Providing the highest quality of services</small>
</h1>
<ol class="breadcrumb">
<li><a href="index.html">Home</a>
</li>
<li class="active">Contact</li>
</ol>
</div>
</div>
<!-- /.row -->
<!--Support Form here-->
<div class="row">
<div class="col-md-8">
<h3>Email & web support</h3>
<div class="center status alert">{{ result }}</div>
<form method='POST' action='/support/'> {% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-success" value="Send Message" />
</form>
<!--Support Form ends here-->
</div>
</div>
<!--Fomr div-s end here-->
<!-- /.row -->
<hr>
<!-- Footer -->
<footer>
<div class="row">
<div class="col-lg-6">
<p>Connect to us on:</p>
<ul class="list-unstyled list-inline list-social-icons">
<li>
<a href="#"><i class="fa fa-linkedin-square fa-2x"></i></a>
</li>
<li>
<a href="#"><i class="fa fa-facebook-square fa-2x"></i></a>
</li>
</ul>
</div>
</div>
</footer>
</div>
<!-- /.container -->
</section>
{% endblock %}
表单按钮“发送消息”没有响应。没有回溯错误。该表格没有保存在数据库中,也没有作为电子邮件发送。 我哪里错了?
【问题讨论】:
-
您可以在您的
if-else分支中添加一些print语句以查看您的代码流向何处吗?顺便说一句,您使用的是模型表单吗? -
是的,我正在使用模型表单。
-
试过打印语句,但我想它甚至不会进入views.py,因为打印语句不起作用。提交按钮没有响应。
-
@SomdipDey 你在
base.html中是否有某种用于form的jQuery 处理程序,它可能会阻止表单提交事件? -
尝试将
input替换为button例如<button type="submit" class="btn btn-success" value="Send Message" />看看是否可行?
标签: django html django-forms