12

Is there a way to programmatically add URL Patterns to Django without having to restart the server?

Or is there a way force Django to reprocess/cache URL patterns ( the URLconf )?

14
  • 1
    Are you using mod_wsgi? If so, you can restart your app without restarting the entire Apache server. Commented Feb 1, 2011 at 18:58
  • @S.Lott for the production server yes, so this might work, how do you restart a specific app? Is there anything similar for the dev server? Commented Feb 1, 2011 at 19:01
  • @S.Lott, For 'dev server' I mean the buit-in development server that comes with Django, eg: python manage.py runserver. I may have misunderstood your first comment though, were you suggesting it is possible to restart a single Django App without restarting the other Apps, or were you suggesting you can restart Django/All apps without having to restart Apache? I'd need to do the former. Commented Feb 1, 2011 at 19:09
  • The built-in development server that comes with Django restarts itself when you make a code change. If you use mod_wsgi, the production server restarts itself when you touch the wsgi file. Commented Feb 1, 2011 at 19:25
  • 1
    "programmatic"? What can that possibly mean? The urls.py is read exactly once when Django starts up and cannot be re-read without restarting Django. Commented Feb 1, 2011 at 19:48

4 Answers 4

4

If you use gunicorn without code preloading, just send a HUP to the gunicorn master process, it will spawn new workers which load the new code, and gracefully shut down the old ones, without a single lost request!

Sign up to request clarification or add additional context in comments.

Comments

3

I tried something like this by hacking some things in django.core.urlresolvers – it worked for me but note that it's a hack. I don't still have the code, but I did something like this:

  1. Django typically uses urlresolvers.get_resolver() to get a RegexURLResolver which is responsible for resolving URLs. Passing None to this function gets your "root" URLConf.
  2. get_resolver() uses a cache, _resolver_cache, for loaded URLConfs.
  3. Resetting _resolver_cache should force Django to recreate a clean URLResolver.

Alternatively, you might try to unset the _urlconf_module attribute of the root RegexURLResolver, which should force Django to reload it (not sure about this though, it's possible that the module will be cached by Python).

from urlresolvers import get_resolver

delattr(get_resolver(None), '_urlconf_module')

Again, not guaranteed that this will work (I'm working from memory from code that I've obviously discarded for some reason). But django/core/urlresolvers.py is definitely the file you'll want to look at.

EDIT: Decided to experiment some with this and it didn't work...

EDIT2:

As I thought, your URL modules will be cached by Python. Simply reloading them as they change might work (using reload). If, your problem is that you're dynamically constructing urlpatterns based on some data which might change.

I tried reloading my root URLs (project.urls) and a suburl module (app.urls). That's all I had to do for the new URLs to be displayed by get_resolver(None).url_patterns

So the trick might be this simple: Manually reload your URL module.

2 Comments

When I get the chance I will play around with this and see if it works.
how do you reload? where this reload function is coming from? This is not clear
3

Here is your answer :

import sys
from django.conf import settings
from importlib import reload
from django.urls import clear_url_caches

urlconf = settings.ROOT_URLCONF
  if urlconf in sys.modules:
      clear_url_caches()
      reload(sys.modules[urlconf])

Comments

1

This seams also an elegant way to do it :

http://codeinthehole.com/writing/how-to-reload-djangos-url-config/

Simply do that to reload your root urls module :

reload_urlconf()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.