我是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 %}
表单按钮&#34;发送消息&#34;没有回应。没有回溯错误。表单不会保存在数据库中,也不会作为电子邮件发送。 我哪里错了?
答案 0 :(得分:1)
以下代码确实有效:
<!--Support Form here-->
<div class="row">
<div class="col-md-8">
<h3>Telephone Support</h3>
</div>
<div class="col-md-6">
<h3>Email & Web Support</h3>
<div class="center status alert alert-success">{{ result }}</div>
<form method='POST' action='/support/'> {% csrf_token %}
<!--{{ form.as_p }}--><!--Commented by SOmdip because form is drawn by hand instead-->
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.name.errors }}
<label for="{{ form.name.id_for_label }}">Name:</label><br />
{{ form.name }}
</div>
<div class="fieldWrapper">
{{ form.email.errors }}
<label for="{{ form.email.id_for_label }}">Email:</label><br />
{{ form.email }}
</div>
<div class="fieldWrapper">
{{ form.telephone.errors }}
<label for="{{ form.telephone.id_for_label }}">Telephone:</label><br />
{{ form.telephone }}
</div>
<div class="fieldWrapper">
{{ form.message.errors }}
<label for="{{ form.message.id_for_label }}">Message:</label><br />
{{ form.message }}
</div>
<input type="submit" class="btn btn-success" value="Send Message" />
</form>
<!--Support Form ends here-->