math tag
Hi, It always frustrates me when I must resort to custom filter to do simple math to achieve some special-case markups (eg. applying a row tag around 5 items max for a list of objects). Do you think a math tag like this that I just published to djangosnippets.org can be made into the default list of tags? http://djangosnippets.org/snippets/2424/ At a glance, 'math' tag supercedes both 'add' and 'divisibleby' default filters. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: math tag
On May 3, 7:43 am, Russell Keith-Magee wrote: > This stems back to the design motivation of Django's template language > -- you shouldn't be doing math in the template. Instead, you should be > doing your math in the view, and providing the template with a > pre-calculated result. > > So, my inclination would be to say no, this doesn't have a place in > the default list of tags. > > Yours, > Russ Magee %-) The example I provided is a bad one, but the math tag is general purpose. It can potentially reduce the number of custom filters or amount of work needed in the view function. Here's another take: {% load mathtag %} {% with x=request.GET.x y=request.GET.y z=request.GET.z %} if x = {{ x }}, y = {{ y }}, {{ x }} + {{ y }} = {% math x y "$1 * $2" as result %}{{ result }} if x = {{ x }}, y = {{ y }}, {{ x }} * {{ y }} = {% math x y "$1 * $2" as result %}{{ result }} if x = {{ x }}, y = {{ y }}, z = {{ z }}, ({{ x }}^{{ z }}) * ({{ y }}^{{ z }}) = {% math x y z "($1**$3) * ($2**$3)" as result %} {{ result }} if x = {{ x }}, y = {{ y }}, z = {{ z }}, ({{ x }} + {{ y }}) / {{ z }} = {% math x y z "($1 + $2) / $3" as result %}{{ result }} {% endwith %} Given the frequency of such request by other users warrants a re-look IMO. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: math tag
> Holy god, not to be rude, but given that this is completely unreadable I'm > even more -1 than I ever would have been on the basis of the principle of a > dumber template language. > > Alex My apologies. All that is doing is rendering the following result when the URL is http://localhost:8001/?x=2&y=3&z=4 if x = 2, y = 3, 2 + 3 = 6 if x = 2, y = 3, 2 * 3 = 6 if x = 2, y = 3, z = 4, (2^4) * (3^4) = 1296 if x = 2, y = 3, z = 4, (2 + 3) / 4 = 1 -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: math tag
> > Or, in a sane world: > > if x = {{ x }}, y = {{ y }}, {{ x }} + {{ y }} = {{ x_plus_y_res }} > if x = {{ x }}, y = {{ y }}, {{ x }} * {{ y }} = {{ x_star_y_res }} > > and so on. > While it is a common consensus that logic should never be in the template, the "solution" on the other hand is inconvenient at best. I must admit the examples are badly written and do not reflect the actual problem. The point here though, is that a general purpose math tag should be available by default. *The* math tag may be too flexible for its own good. If we have a dumber solution, I'm fine with that so long it supports the simplest cases, and that would be + - * / % min max and (). One real world problem that I always have is trying to output Javascript with Django template. Precomputing the values inside the view and pass them into the template is annoyingly redundant when the values can be derived from other objects in the context. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: Django urls in JavaScript
> Weird, I have missed this thread. But anyway, like sdcooke, that's > also the way that we handle URLs in javascript. > Attach a data attribute to the HTML node to which it applies, and read > it from inside the javascript. It's clean. > > x:ajax-url="{% url accounts_ajax_edit_name %}">{% trans "Edit" %} a> > > Like gettext, a seperate, dynamically generated javascript file for > URL resolving is not scalable to lange web applications. > And further, I think that the urls and names of views are not meant to > be exposed to the client. I don't want a visitor to be able to reverse > engineer my website, and read all the possible URL patterns. > > Jonathan Just sharing thought here. Another approach that I use is creating an "indirect" url and view that reads the url_name and args from request.REQUEST, reverse the path, resolve the view and call it on your behalf. ## urls.py # from django.conf.urls.defaults import patterns, url urlpatterns = patterns('js.views', url(r'^url_reverse/$', 'url_reverse', name='js_url_reverse'), ) ## views.py # from django import http from django.core import urlresolvers def url_reverse(request): if request.method in ('GET', 'POST'): data = getattr(request, request.method) url_name = data.get('url_name') try: path = urlresolvers.reverse(url_name, args=data.getlist('args')) (view_func, args, kwargs) = urlresolvers.resolve(path) return view_func(request, *args, **kwargs) except urlresolvers.NoReverseMatch: raise http.Http404 return http.HttpResponseNotAllowed(('GET', 'POST')) Somewhere in javascript: $.get('/js/url_reverse/', { url_name: "account_profile", args: [username]}); Of cause, this approach exposes your url names to javascript. Some people might not like it. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.