开发者

Submit button still sends email

开发者 https://www.devze.com 2023-02-13 08:43 出处:网络
I have a view called send_confirmation email - which basically sends emails. I want to add a button that can delete an order no. on that same form. Unfortunately. If I add a button that says to delete

I have a view called send_confirmation email - which basically sends emails. I want to add a button that can delete an order no. on that same form. Unfortunately. If I add a button that says to delete that order, then, for some reason, my program thinks that this is the send mail button and will instead send an email even though I have set that button to delete the Order. I just want that button to delete the order number without the email getting sent.

def send_confirmation_email(request, order_no = 0, service_type = 0):
    order = None
    count = 0
    title = models.SERVICE_CHOICE开发者_运维问答S[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 %}


First of all, your code would submit a confirmation regardless of delete.

Second, your request.POST.get('delete') is failing, because there is no 'delete' in POST.

Set the submit button name attribute to '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:

            ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号