Python DeliveryForm Example

说明

python deliveryform示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Python

命名空间/包名称: pizzaforms

示例#1
文件: views.py项目: kirillsavelyev/python-tceh

def create(request):
    if request.method == 'GET':
        c = RequestContext(request, {
            'pizza_form': PizzaOrderForm(),
            'delivery_form': DeliveryForm(),
        })
        return render_to_response('pizza/create.html', c)

    elif request.method == 'POST':
        pizza_form = PizzaOrderForm(request.POST)
        delivery_from = DeliveryForm(request.POST)

        if pizza_form.is_valid() and delivery_from.is_valid():
            user = request.user
            user = user if user.is_authenticated() else None
            with transaction.atomic():
                delivery = delivery_from.save(user=user)
                pizza = pizza_form.save(delivery=delivery)
                pizza_form.save_m2m()

            return redirect(reverse('pizza:view', kwargs={
                'pizza_order_id': pizza.pk
            }))
        else:
            c = RequestContext(request, {
                'pizza_form': pizza_form,
                'delivery_form': delivery_from,
            })
            return render_to_response('pizza/create.html', c)
    return HttpResponse(status=405)

示例#2
文件: views.py项目: andyzt/tceh

def create(request):
    """
    This view renders a form for creating new :class:`PizzaOrder`
    or accepts POST requests with the form to save its data. Or render errors.

    PizzaOrder: is a base class that contains all order data, see field list below:

    - kind
    - size
    - delivery
    - extra
    - exclude
    - comment
    - delivered
    - date_created
    - date_delivered

    Args:
        request: HttpRequest, accepted methods: GET, POST

    Returns:
        HttpResponse with rendered view, or error code if used method other than GET or POST
    """
    if request.method == 'GET':
        c = RequestContext(request, {
            'pizza_form': PizzaOrderForm(),
            'delivery_form': DeliveryForm(),
        })
        return render_to_response('pizza/create.html', c)

    elif request.method == 'POST':
        pizza_form = PizzaOrderForm(request.POST)
        delivery_from = DeliveryForm(request.POST)

        if pizza_form.is_valid() and delivery_from.is_valid():
            user = request.user
            user = user if user.is_authenticated() else None
            with transaction.atomic():
                delivery = delivery_from.save(user=user)
                pizza = pizza_form.save(delivery=delivery)
                pizza_form.save_m2m()

            return redirect(reverse('pizza:view', kwargs={
                'pizza_order_id': pizza.pk
            }))
        else:
            c = RequestContext(request, {
                'pizza_form': pizza_form,
                'delivery_form': delivery_from,
            })
            return render_to_response('pizza/create.html', c)
    return HttpResponse(status=405)

示例#3
文件: views.py项目: kirillsavelyev/python-tceh

def create(request):
    """
    This view renders a form for creating new :class:`PizzaOrder` (pizza/create.html)
    or accepts POST requests with the form to save its data.

    Args:
        request: HttpRequest, accepted methods: GET, POST

    Returns:
        * HttpResponse with rendered view form for new order
        * Status code 405 if request method not GET
    """

    if request.method == 'GET':
        c = RequestContext(request, {
            'pizza_form': PizzaOrderForm(),
            'delivery_form': DeliveryForm(),
        })
        return render_to_response('pizza/create.html', c)

    elif request.method == 'POST':
        pizza_form = PizzaOrderForm(request.POST)
        delivery_from = DeliveryForm(request.POST)

        if pizza_form.is_valid() and delivery_from.is_valid():
            user = request.user
            user = user if user.is_authenticated() else None
            with transaction.atomic():
                delivery = delivery_from.save(user=user)
                pizza = pizza_form.save(delivery=delivery)
                pizza_form.save_m2m()

            return redirect(reverse('pizza:view', kwargs={
                'pizza_order_id': pizza.pk
            }))
        else:
            c = RequestContext(request, {
                'pizza_form': pizza_form,
                'delivery_form': delivery_from,
            })
            return render_to_response('pizza/create.html', c)
    return HttpResponse(status=405)

展开阅读全文