I'm trying to render out the get_cart_total to the email template I'm working on, which gets sent when the success view is triggered and the boolean is True, currently it gets an error message saying 'WSGIRequest' object has no attribute 'order', what's causing this and how do I fix it? Model
class Customer(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, blank=True, null=True)
email = models.EmailField(max_length=150)
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
@property
def get_cart_total(self):
orderitems = self.orderitem_set.all()
total = sum([item.get_total for item in orderitems])
return total
Views
def success(request):
if order.complete == True:
email = request.user.customer.email
#error
cart_total = request.order.get_cart_total
html_message = render_to_string('check/emails/test.html', {'cart_total': cart_total})
recipient_list = [email, ]
send_mail( subject, plain_message, email_from, recipient_list, html_message=html_message )
Traceback
Traceback (most recent call last):
File "D:\test\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "D:\test\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\test\shop\views.py", line 364, in success
cart_total = request.order.get_cart_total
AttributeError: 'WSGIRequest' object has no attribute 'order'
CodePudding user response:
You are trying to access order from request. TRy to remove request ?
def success(request):
if order.complete == True:
email = request.user.customer.email
#error
cart_total = order.get_cart_total
html_message = render_to_string('check/emails/test.html', {'cart_total': cart_total})
recipient_list = [email, ]
send_mail( subject, plain_message, email_from, recipient_list, html_message=html_message )