English 中文(简体)
Django: Passing a request directly (inline) to a second view
原标题:

I m trying to call a view directly from another (if this is 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 the DB for the 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 does it I was wondering if I could just call the first view from the second one.

What I m aiming for is just passing through the request object to the second view and return the obtained response object in turn back to the client.

Any help greatly appreciated, critism as well if this is a bad way to do it. But then some pointers .. to avoid DRY-ing.

Thanx!

Gerard.

最佳回答

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]) )
问题回答

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.

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).





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签