views:

20

answers:

1

I have a Django app where users can select between 2 interface modes, that mode affect some pages... for those pages I use different templates

In urls.py I have something like this:

mode = Config.objects.get().mode
urlpatterns = patterns('',
    url(r'^my_url/$', 'custom_view', {'template':'my_template.html', 'mode':mode} ),
)

Then my view is something like this:

@render_to()
def custom_view(request, template, mg=False, login=True):
    if mode:
        template = template + 'x' #I add an x to the template name to advice to django I that it should use the mode_2 template.
    return {'TEMPLATE':template}

My problem is when the user selects mode 2 (in my custom Configuration page), mode does not change until server is restarted (either apache or runserver.py is the same).

I think this has to do something with cache, but I can't find how to erase that cache. (each time Config.mode is changed.)

+3  A: 

Getting the mode in urls.py is not going to work. The get will only be executed once, when the file is first imported.

Do the database work in the view function, instead.

Daniel Roseman
+1. `urls.py` is loaded only once. It is not the best place for dynamic configuration.
Manoj Govindan
+1. We have a `startup.py` which is imported by `urls.py` and used to contain the one-time startup script.
S.Lott