Over a million developers have joined DZone.

How to Reload Django's URL Config

DZone's Guide to

How to Reload Django's URL Config

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.


For some reason, you need to reload your Django URL config.

Normally, the root URL config will be imported and stored in memory when your server process starts up. Occasionally though, you may want to reload it. This can be the case if your URL configuration changes depending on certain parameters.


You can reload the URL config using the following snippet:

import sys
from django.conf import settings

def reload_urlconf(urlconf=None):
    if urlconf is None:
        urlconf = settings.ROOT_URLCONF
    if urlconf in sys.modules:


This was a problem I needed to solve while testing the checkout process for django-oscar. Oscar uses a setting flag to optionally add decorators to certain URLs, and hence I needed to patch the setting and reload the URLs as part of the set-up for a test.

This was achieved in the following way, where the setting OSCAR_ALLOW_ANON_CHECKOUT is set to True and the URL config is reloaded.

import sys
from django.conf import settings


class EnabledAnonymousCheckoutViewsTests(ClientTestCase, CheckoutMixin):

    def reload_urlconf(self):
        if settings.ROOT_URLCONF in sys.modules:
        return import_module(settings.ROOT_URLCONF)

    def test_shipping_address_requires_session_email_address(self):
        with patch_settings(OSCAR_ALLOW_ANON_CHECKOUT=True):
            response = self.client.get(reverse('checkout:shipping-address'))

There's probably a better way of doing this, but it wasn't apparent to me when working on this problem today.


Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of David Winterbottom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}