DZone
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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Top Alternatives to Django for Web Development
  • Flask vs. Django: Which Python Framework to Choose?
  • Django: Context Processors for Custom Environments
  • Async Support in Django

Trending

  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  • Continuous Integration vs. Continuous Deployment
  • Development of Custom Web Applications Within SAP Business Technology Platform
  • The Systemic Process of Debugging
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Reload Django's URL Config

How to Reload Django's URL Config

David Winterbottom user avatar by
David Winterbottom
·
Apr. 01, 12 · Interview
Like (0)
Save
Tweet
Share
6.33K 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.

Related

  • Top Alternatives to Django for Web Development
  • Flask vs. Django: Which Python Framework to Choose?
  • Django: Context Processors for Custom Environments
  • Async Support in Django

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: