DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How to Reload Django's URL Config

How to Reload Django's URL Config

David Winterbottom user avatar by
David Winterbottom
·
Apr. 01, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
6.02K Views

Join the DZone community and get the full member experience.

Join For Free

Problem

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.

Solution

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:
        reload(sys.modules[urlconf])

Discussion

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:
            reload(sys.modules[settings.ROOT_URLCONF])
        return import_module(settings.ROOT_URLCONF)

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

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

 

Django (web framework)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Event-Driven Microservices?
  • Progressive Web Apps vs Native Apps: Differences and Similarities
  • What Emerging Technologies Make Data Centers More Energy Efficient?
  • How to Build Microservices With Node.js

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo