On Mon, Mar 11, 2013 at 2:21 PM, Tom Evans <[email protected]> wrote:
> On Mon, Mar 11, 2013 at 2:00 PM, jayhalleaux <[email protected]> wrote:
>>>> Any ideas on question 1? how to use named urls in settings.py?
>>
>
> The problem with having named URLs in settings.py (also some other
> places, models.py for instance) is that at this point, the URL
> resolving architecture has not yet been completed.
>
> There is a simple solution - you do not actually want the URL in
> settings.py at the time settings.py is parsed. When you actually want
> the URL, everything is properly set up. Therefore, you can use a
> simply lazy evaluation of your reverse call:
>
>   from django.utils.functional import lazy
>   from django.core.urlresolvers import reverse
>
>   lazy_reverse = lazy(reverse, str)
>
>   FOO_URL = lazy_reverse('mysite:my-named-url')
>
> The downside of lazy is that this url will get re-generated each time
> you access FOO_URL. There is a bit of magic for that too, you can
> memoize the result:
>
>   from django.utils.functional import lazy, memoize
>   from django.core.urlresolvers import reverse
>
>   _reverse_memo_cache = { }
>   lazy_memoized_reverse = memoize(lazy(reverse, str), _reverse_memo_cache, 1)
>
>   FOO_URL = lazy_memoized_reverse('mysite:my-named-url')
>
> Cheers
>
> Tom

Just noticed, I have my memoize and lazy the wrong way around, so it
has the wrong effect! The correct way should be:

  lazy_memoized_reverse = lazy(memoize(reverse, c2, 1), str)

IE, memoize the return value of reverse, and lazy evaluate it, not
memoize the lazily evaluated reverse function!

How embarrassing :)

Cheers

Tom

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to