|
| 1 | +import importlib |
| 2 | +import logging |
| 3 | + |
| 4 | +from django.apps import apps |
| 5 | +from django.conf import settings |
1 | 6 | from django.conf.urls import url, include |
| 7 | +from django.conf.urls.static import static |
2 | 8 | from django.contrib import admin |
| 9 | +from django.urls import path |
| 10 | + |
| 11 | +urlpatterns = [url(r"admin/", admin.site.urls)] + ( |
| 12 | + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
| 13 | + if settings.MEDIA_ROOT |
| 14 | + else [] |
| 15 | +) |
| 16 | + |
| 17 | +""" |
| 18 | +Here, apps are registered automatically with their urls if they have a property "url_prefix" set in their AppConfig |
| 19 | +AND if they have a valid urls.py file which sets a variable urlpatterns |
| 20 | +""" |
| 21 | +for config in apps.get_app_configs(): |
| 22 | + if hasattr(config, "url_prefix"): |
| 23 | + logging.debug(f"Found url_prefix in {config.name}, checking for .urls module") |
| 24 | + urls_path = config.module.__name__ + ".urls" |
| 25 | + try: |
| 26 | + mod = importlib.import_module(urls_path) |
| 27 | + except ModuleNotFoundError: |
| 28 | + logging.debug(f"No url module found under {urls_path}", exc_info=True) |
| 29 | + continue |
| 30 | + |
| 31 | + logging.debug( |
| 32 | + f"urls.py present under {urls_path}, setting for prefix {config.url_prefix}" |
| 33 | + ) |
| 34 | + # Do the include |
| 35 | + urlpatterns.append(path(config.url_prefix + "/", include(urls_path))) |
3 | 36 |
|
4 | | -urlpatterns = [ |
5 | | - url("", include("omap.core.urls")), |
6 | | - url(r"admin/", admin.site.urls), |
7 | | -] |
| 37 | +logging.debug(f"Patterns: {urlpatterns}") |
0 commit comments