Call method of the session template

73 views
Skip to first unread message

Ricardo

unread,
Oct 10, 2013, 1:29:35 PM10/10/13
to django...@googlegroups.com
Hi, I'm using django carton to shopping cart.
I am not able to call the method remove_single in my template. on line 15
I tried several ways but I can not.
If you can help

carton      ---> http://pastebin.com/X4FtHRZU

thank you
Ricardo

Bill Freeman

unread,
Oct 10, 2013, 3:06:42 PM10/10/13
to django-users
The remove method requires an argument.  You're not supplying one (you can't in a template variable reference).


--
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...@googlegroups.com.
To post to this group, send email to django...@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/b67afb27-366b-4570-85a6-58900322fa3e%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ricardo Kamada

unread,
Oct 10, 2013, 3:17:58 PM10/10/13
to django...@googlegroups.com
I tried numerous times ...
but if I put that way 
{% 'item.remove_single' item.product %} or {% 'item.remove_single' product= item.product %}
blames this error " Invalid block tag: ''item.remove_single'', expected 'empty' ou 'endfor' "

Ricardo


2013/10/10 Bill Freeman <ke1...@gmail.com>

--
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...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Bill Freeman

unread,
Oct 10, 2013, 3:29:32 PM10/10/13
to django-users
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


Daniel Roseman

unread,
Oct 11, 2013, 4:05:27 AM10/11/13
to django...@googlegroups.com
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.

Ricardo Kamada

unread,
Oct 11, 2013, 8:45:01 AM10/11/13
to django...@googlegroups.com
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 <dan...@roseman.org.uk>

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Daniel Roseman

unread,
Oct 11, 2013, 10:49:10 AM10/11/13
to django...@googlegroups.com
On Friday, 11 October 2013 13:45:01 UTC+1, Ricardo wrote:
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.


It makes absolutely no difference if it lives in the session, you still can only modify it by calling a URL on the server. By the time the user sees the template, it is rendered to just HTML. The HTML in the user's browser does not have access to the methods of a session object which lives on the server. The only thing you can do from the browser is call a URL.

Now, if you like, you can call that URL via Ajax, so the user doesn't actually have to navigate to another page: but the fact remains, the only way to modify something on the server - whether in the session, the db, or even a file on disk - is by calling a URL.
--
DR.
Reply all
Reply to author
Forward
0 new messages