Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 23, 2010 23:23:27

Luke M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

{% url %} and None


I have a few pages essentially implementing a master/detail view; it's a
report with variably many different constraints on the data. I'm making
thorough use of Django's flexible URL configuration — I have just one view,
defined by one (rather long) regex with multiple optional capturing groups.
For illustration's sake, let's say it's this:


/widgetStore/productLine/(?P<productLineId>\d+)/report/
(?:region/(?P<regionId>\d+)/)?

(?:color/(?P<color>\w+)/)?

(?:size/(?P<size>\w+)/)?$


(without the newlines, of course). The view function looks something like:

def productLineReport(request, productLineId, regionId=None, color=None,
size=None):

# ...


This was all very convenient and (IMO) elegant thanks to Django. Now, in the
template, I want to have links that add or remove parameters to the URL,
regardless of which ones are there to begin with. So:

<a href="{% url widgetStore.views.productLineReport
productLineId=productLineId regionId=regionId color='blue' size=size %}">
Blue
</a>


But this doesn't work, because productLineId and size can be None, in which
case the URL looks something like
“/widgetStore/productLine/42/report/region/None/color/blue/size/None”. I
want to leave out those parameters altogether if the value is None.

(Actually, in the above case, I get a NoReverseMatch exception because
“None” doesn't match the regex for the productId capturing group.)

In fact, I don't see much of a use case for the behavior of reverse URL
lookups when None appears in keyword parameters; does anyone really want
“None” to appear literally in their URLs? To me, passing None as the value
of a keyword parameter to reverse() or {% url %} pretty unambiguously means
“leave off this parameter.” And the way it currently works, there's no way
to use {% url %} to build a URL that may or may not include optional
parameters; the only solution (without writing a custom tag) would be to
have several different tags and choose among them with {% if %}, which is
infeasible in any but the simplest cases (in which it's merely awkward).

Looking at the code, I see it wouldn't be much effort to implement this
change in behavior; my workaround was to rip the code for {% url %} and
change two lines in URLNode.resolve(). Alternately, one could change
urlresolvers.reverse() almost as simply to keep the behavior consistent
globally. Does this make sense to people? Should I go file a bug (possibly
with very short proposed patch)?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 19th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support