Django: passing request directly (inline) to second view

I am trying to call a view directly from another (if at all possible). I have a view:

def product_add(request, order_id=None):
    # Works. Handles a normal POST check and form submission and redirects
    # to another page if the form is properly validated.

Then I have a 2nd view that queries DB for product data and should call the first one.

def product_copy_from_history(request, order_id=None, product_id=None):
    product = Product.objects.get(owner=request.user, pk=product_id)

    # I need to somehow setup a form with the product data so that the first
    # view thinks it gets a post request. 
    2nd_response = product_add(request, order_id)
    return 2nd_response

Since the second one needs to add the product as the first view, I was wondering if I could just call up the first view from the second.

What I'm aiming for is simply to pass the request object to the second view and return the received response object back to the client.

Any help is much appreciated, criticism also if this is a bad way to do it. But then some pointers .. to avoid drying.

Thanx!

Gerard.

+3
3

, . :

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]) )
+3

python, , , , (, 404...). , , . utiliy .

0

API HTTP, urllib to post product_add.

, , dev, django, (. trac, google).

0
source

Source: https://habr.com/ru/post/1725483/


All Articles