order_create()只需要2个参数(给定1个)

时间:2017-10-31 17:02:45

标签: django celery

我正在尝试做芹菜任务,代码如下。

task.py

from .models import OrderItem
from cart.cart import Cart
from .forms import OrderCreateForm

@task(name="create_order")
def create_order(request):
    cart = Cart(request)
    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart:
                try:
                    OrderItem.objects.create(order=order,
                                             product=item['product'],
                                             price=item['price'],
                                             quantity=item['quantity'])
                except:
                    pass
                cart.clear()
            return None
    else:
        form = OrderCreateForm()
    return None

views.py

from .models import OrderItem, Order
from cart.cart import Cart
from .tasks import create_order


def order_create(request, order_id):
    order = Order.objects.get(id=order_id)
    cart = Cart(request)
    create_order.delay(order.id)
    return render(request,'orders/order_created.html', {'cart': cart, 'order': order})

urls.py

from .views import order_create


urlpatterns = [
    url(r'^create/$',order_create, name='order_create'),
    ]

执行代码时,我收到错误' order_create()只需要2个参数(给定1个)'。希望有人能帮助我解决它。谢谢。

以下是我的工作代码,它在创建订单后发送邮件,但我想将整个视图功能更改为芹菜任务。

@task(name="send_mail_to_custormer")
def order_created(order_id):
    order = Order.objects.get(id=order_id)
    subject = 'Order nr. {}'.format(order.id)
    message = 'Dear {} {},\n\nYou have successfully placed an order. Your order id is {}'.format(order.first_name,
                                                        order.last_name, order.id)
    from_email = settings.EMAIL_HOST_USER
    to_email = [order.email]
    mail_sent = send_mail(
                            subject,
                            message,
                            from_email,
                            to_email,
                            fail_silently=False
                          )
    return mail_sent

views.py

def order_create(request):
cart = Cart(request)
if request.method == 'POST':
    form = OrderCreateForm(request.POST)
    if form.is_valid():
        order = form.save()
        for item in cart:
            try:
                OrderItem.objects.create(order=order,
                                         product=item['product'],
                                         price=item['price'],
                                         quantity=item['quantity'])
            except:
                pass
            cart.clear()
            order_created.delay(order.id)
        return render(request,'orders/order_created.html', {'cart': cart, 'order': order})
else:
    form = OrderCreateForm()
return render(request, 'orders/order_create.html', {'cart': cart, 'form': form})

1 个答案:

答案 0 :(得分:0)

将网址定义更改为以下网址,您错过了将order_id作为参数传递给视图

url(r'^create/(?P<order_id>\w+)/$',order_create, name='order_create'),

你应该使用url传递order_id。