我有一个名为send_confirmation的视图 - 基本上发送电子邮件。我想添加一个可以删除订单号的按钮。在同一个表格上。不幸。如果我添加一个删除该订单的按钮,那么,由于某种原因,我的程序认为这是发送邮件按钮,即使我已设置该按钮删除订单,也会发送电子邮件。我只是希望该按钮删除订单号而不发送电子邮件。
def send_confirmation_email(request, order_no = 0, service_type = 0):
order = None
count = 0
title = models.SERVICE_CHOICES[int(service_type) - 1][1]
#title = type[1]
order_number = request.session['order_number']
if request.POST.get('delete'):
order_number.delete()
try:
order = models.Order.objects.get(pk = order_no)
count = order.orderservicelist_set.count()
if request.method == 'POST':
email = EmailMessage(subject = 'Order Confirmation')
email.to = [order.contact.email, request.user.email]
email.body = request.POST.get('email-message', '')
response = print_order(request, order_no)
#email.attach('order_details.pdf', response.content, 'application/pdf')
email.send(fail_silently = False)
request.user.message_set.create(message = "Email confirmation sent!")
return HttpResponseRedirect(reverse(return_clients))
except:
return HttpResponseRedirect(reverse(return_clients))
return render_to_response('order_confirmation.html', {'order':order, 'title':title, 'count':count, 'order_number':order_number}, context_instance = RequestContext(request))
{% block right_content %}
<div id="location_header">Confirmation email</div>
<form action="." method="post">
<div class="form_container">
<fieldset class="model"><legend>Email body</legend>
<br>
<textarea name="email-message" rows="20">
{{title}} Order Confirmation
Date : {{ order.date|date:"F d, Y" }}
<div id="form_footer">
` <input type="submit" value="Send email">
<input type="submit" value="delete" value="Delete Order">
</div>
</form>
{{order_number.pk}}
{% endblock %}
答案 0 :(得分:0)
首先,无论删除,您的代码都会提交确认。
其次,您的request.POST.get('delete')
失败,因为POST中没有'delete'
。
将提交按钮name
属性设置为'delete'
:<input type="submit" name="delete" />
def send_confirmation_email(request, order_no = 0, service_type = 0):
order = None
count = 0
title = models.SERVICE_CHOICES[int(service_type) - 1][1]
#title = type[1]
order_number = request.session['order_number']
if request.POST.get('delete'):
order_number.delete()
else: # else or else you will always get a confirmation regardless of delete
# unless of course that's what you want.
try:
...