Friday, October 11, 2013

Re: Call method of the session template

I understand what you say, however I know only call methods using {% url 'app.view.func'%} or {%}% app.view.func.
As I understand this django-aap carton, the shopping cart in the session live. If it is in session I would have
access their methods and attributes. Well that's exactly what I'm not getting. Access method remove_single.
In this href = "" is an image that removes the items you want to call the method remove_simgle.
Actually I'm wrong in doing {% url%} because I do not want move pages. I just want to remove the item from the cart.


class Cart(object):
    """
    A cart that lives in the session.
    """
    #...
    #...

    def remove_single(self, product):
        """
        Removes a single product by decreasing the quantity.
        """
        if product not in self.products:
            return
        if self._items_dict[product.pk].quantity <= 1:
            # There's only 1 product left so we drop it
            del self._items_dict[product.pk]
        else:
            self._items_dict[product.pk].quantity -= 1
        self.session.modified = True

Ricardo


2013/10/11 Daniel Roseman <daniel@roseman.org.uk>
On Thursday, 10 October 2013 20:29:32 UTC+1, ke1g wrote:
No.  You can't pass an argument to a model method from the template.  I see two options for you:

1.  Rework the method so that the argument is optional, and if the argument is not provided, the method uses a reasonable default (maybe item.product?).
2. Write a custom template filter that lets you supply an argument

These answers all miss the point. The main problem is not that you need to pass an argument, it's that what you're trying to do makes *no sense at all* in the context of a template.

You can't make the href of a link just point to a model method. A link needs to be a URL. So it needs a view, and an entry in the urlconf pointing to that view. That urlconf entry, of course, can take an argument, but that argument can't be a "product", it needs to be some kind of ID or slug that can be represented in the URL. 

Once you've got the view and the URL, you can use the `{% url %}` tag to generate the value for your link:

    <a href="{% url "remove_single" item.id %}">

(although note that an action that removes an item should be a POST, not a simple GET link).
--
DR.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/AzIxhSQpGws/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/acd5df91-ac8d-4141-a6fc-ccf4447b6b8d%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJTdXTEQD_RyBWNs9vkrE8YuRkCmSP74nQ8vBL5qSGYeXcEWpQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate