Overview
The URLs API provides functions to dynamically discover and extend URL patterns from SuperApp modules. This allows each SuperApp to define its own URL routes that are automatically integrated into your Django project’s URL configuration.extend_superapp_urlpatterns
Automatically discovers and extends URL patterns from all SuperApp modules within a package.Parameters
The main Django URL patterns list to be extended. This is typically your project’s
urlpatterns list from urls.py.The package containing your SuperApp modules. The function will iterate through all modules in this package to discover URL patterns.
Implementation Details
The function performs the following operations (lines 7-19 ofurls.py):
- Module Discovery: Uses
pkgutil.iter_modules()to iterate through all modules in the package - URLs Module Loading: For each module, attempts to import a
urlssubmodule (e.g.,package.module_name.urls) - Graceful Fallback: If a module doesn’t have a urls submodule, it’s silently skipped
- Extension Hook: If the urls module has an
extend_superapp_urlpatternsfunction, it’s called with the main urlpatterns list - Error Handling: Re-raises any import errors that aren’t related to missing urls modules
Return Value
This function modifies the
main_urlpatterns list in-place and does not return a value.Usage Example
urls.py
extend_superapp_admin_urlpatterns
This function is deprecated and will be removed in a future version. Admin URL patterns will be generated in
admin_portal/sites.py instead.Parameters
The main admin URL patterns list to be extended.
The package containing your SuperApp modules.
Implementation Details
The function performs similar operations toextend_superapp_urlpatterns (lines 22-34 of urls.py):
- Module Discovery: Iterates through all modules in the package
- URLs Module Loading: Attempts to import urls submodules
- Admin Extension Hook: Calls
extend_superapp_admin_urlpatternsif present in the urls module
Return Value
This function modifies the
main_admin_urlpatterns list in-place and does not return a value.extend_with_superapp_urlpatterns
Convenience function that combines both regular and admin URL pattern extension.Parameters
The main Django URL patterns list to be extended.
The package containing your SuperApp modules.
Implementation Details
This function is a wrapper that calls both (lines 40-43 ofurls.py):
extend_superapp_urlpatterns(main_urlpatterns, superapp_apps)- Extends regular URL patternsextend_superapp_admin_urlpatterns(main_admin_urlpatterns, superapp_apps)- Extends admin patterns (deprecated)
The admin URL patterns functionality in this function is deprecated and will be removed soon.
Return Value
This function modifies the
main_urlpatterns list in-place and does not return a value.Usage Example
urls.py
SuperApp URL Structure
Each SuperApp module can define its own URL patterns by creating aurls.py file:
superapp_apps/my_app/urls.py
Best Practices
1. Namespacing URLs
2. API Versioning
3. Conditional URL Registration
Example: Multiple SuperApps
Error Handling
All URL extension functions handle errors consistently:- Missing URLs Module: If a SuperApp doesn’t have a
urls.pyfile, the module is skipped silently - Import Errors: Any import errors that aren’t related to missing urls modules are re-raised
- Logging: Uses Python’s logging framework to track issues (via
loggerat module level)