[Django]-Django: Passing a request directly (inline) to a second view

3πŸ‘

βœ…

My god, what was I thinking. This would be the cleanest solution ofcourse:

def product_add_from_history(request, order_id=None, product_id=None):
    """ Add existing product to current order
    """
    order = get_object_or_404(Order, pk=order_id, owner=request.user)
    product = Product.objects.get(owner=request.user, pk=product_id)

    newproduct = Product(
                    owner=request.user,
                    order = order,
                    name = product.name,
                    amount = product.amount,
                    unit_price = product.unit_price,
                    )
    newproduct.save()
    return HttpResponseRedirect(reverse('order-detail', args=[order_id]) )
πŸ‘€GerardJP

0πŸ‘

A view is a regular python method, you can of course call one from another giving you pass proper arguments and handle the result correctly (like 404…). Now if it is a good practice I don’t know. I would myself to an utiliy method and call it from both views.

πŸ‘€Nicolas Goy

0πŸ‘

If you are fine with the overhead of calling your API through HTTP you can use urllib to post a request to your product_add request handler.

As far as I know this could add some troubles if you develop with the dev server that comes with django, as it only handles one request at a time and will block indefinitely (see trac, google groups).

πŸ‘€tosh

Leave a comment