Maxtork
Maxtork

Reputation: 49

sending email from Django view

I have a web app for ordering parts and I want to include an email when the user orders a part. I am having trouble adding the email function to my function based view.

I have an existing function for saving the order form data and I wanted to simply add the email function to the same view. When I try to add it directly I keep getting error messages about inconsistent white space. So I created a new email function just below the new order view but I don't know how to pass some parameters from one function to the other.

This is close but when I call the New_Order function it throws an error for order_instance not defined.

Views.py

@login_required
def New_Order(request):
    if request.user.userprofile.shipment_access:
        if request.method == 'POST':
            form = OrderForm(request.POST)
            if form.is_valid():
                order_instance = form.save(commit=True)
                order_instance.save()
            return Order_Email()
        else:
            form = OrderForm()
        return render(request, 'app/New_Order.html', {'form': form})
    else: 
        raise PermissionDenied

def Order_Email():
    subject = "Part Order" + order_instance.id
    orderNum = order_instance.id
    from_email = settings.EMAIL_HOST_USER
    to_email = ['[email protected]']
    message = order_instance
    send_mail(subject=subject, from_email=from_email, recipient_list=to_email, message=message, fail_silently=False)
    return HttpResponseRedirect('/Orders')

I'm ok with either one combined function or with two functions and passing the parameters from one to the next. If there is a reason I can't add the steps into one function can you explain why? I'm still learning and I'm sure I am missing something in that regard.

Thanks Max

Upvotes: 0

Views: 56

Answers (2)

Nimdeveloper
Nimdeveloper

Reputation: 386

add an argument to Order_Email :

def Order_Email(order_instance):

and pass the value when you call the function:

return Order_Email(order_instance)

so your code will be like this:

@login_required
def New_Order(request):
    if request.user.userprofile.shipment_access:
        if request.method == 'POST':
            form = OrderForm(request.POST)
            if form.is_valid():
                order_instance = form.save(commit=True)
                order_instance.save()
            return Order_Email(order_instance)
        else:
            form = OrderForm()
        return render(request, 'app/New_Order.html', {'form': form})
    else: 
        raise PermissionDenied

def Order_Email(order_instance):
    subject = "Part Order" + order_instance.id
    orderNum = order_instance.id
    from_email = settings.EMAIL_HOST_USER
    to_email = ['[email protected]']
    message = order_instance
    send_mail(subject=subject, from_email=from_email, recipient_list=to_email, message=message, fail_silently=False)
    return HttpResponseRedirect('/Orders')

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599788

That is a completely standard function, just pass parameters to it as you normally would. (Note also, the call would need to be inside the if block.)

        if form.is_valid():
            order_instance = form.save(commit=True)
            order_instance.save()
            return Order_Email(order_instance)
...

def Order_Email(order_instance):

Upvotes: 0

Related Questions