Python-URL-Einstellung enthalten Funktion

Was beinhaltet Include eigentlich in den URL-Einstellungen?

Sie können include () verwenden, um die PATH-Einstellung abzurufen, aber was machen Sie eigentlich?

(Referenz) mysite/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

Originaltext in der eigentlichen Funktion

def include(arg, namespace=None):
    app_name = None
    if isinstance(arg, tuple):
        # Callable returning a namespace hint.
        try:
            urlconf_module, app_name = arg
        except ValueError:
            if namespace:
                raise ImproperlyConfigured(
                    'Cannot override the namespace for a dynamic module that '
                    'provides a namespace.'
                )
            raise ImproperlyConfigured(
                'Passing a %d-tuple to include() is not supported. Pass a '
                '2-tuple containing the list of patterns and app_name, and '
                'provide the namespace argument to include() instead.' % len(arg)
            )
    else:
        # No namespace hint - use manually provided namespace.
        urlconf_module = arg

    if isinstance(urlconf_module, str):
        urlconf_module = import_module(urlconf_module)
    patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
    app_name = getattr(urlconf_module, 'app_name', app_name)
    if namespace and not app_name:
        raise ImproperlyConfigured(
            'Specifying a namespace in include() without providing an app_name '
            'is not supported. Set the app_name attribute in the included '
            'module, or pass a 2-tuple containing the list of patterns and '
            'app_name instead.',
        )
    namespace = namespace or app_name
    # Make sure the patterns can be iterated through (without this, some
    # testcases will break).
    if isinstance(patterns, (list, tuple)):
        for url_pattern in patterns:
            pattern = getattr(url_pattern, 'pattern', None)
            if isinstance(pattern, LocalePrefixPattern):
                raise ImproperlyConfigured(
                    'Using i18n_patterns in an included URLconf is not allowed.'
                )
    return (urlconf_module, app_name, namespace)

Recommended Posts

Python-URL-Einstellung enthalten Funktion
Python-Funktion ①
[Python] -Funktion
Python-Funktion ②
Python-Aufzählungsfunktion
Python> Funktion> Schließen
[Python] Generatorfunktion
Python> Funktion> Innere Funktion
Python-Funktionsdekorateur
Über Funktionsargumente (Python)
Funktionsausführungszeit (Python)
Zusammenfassung der Python-Funktionsargumente
Python-Druckfunktion (Fortsetzung)
Python: Über Funktionsargumente
URL-Verkürzung mit Python
Timefloor-Funktion (Python)
Python Umweltfreundliche japanische Umgebung
Erstellen Sie eine Funktion in Python
Verwenden Sie die Rückruffunktion in Python
Parametereinstellung durch Python Configparser
[Python] Wert des Funktionsobjekts (?)
ntile (Dezil) -Funktion in Python
[Python] Der Ursprung des Namens der Python-Funktion
Python-Anfänger-Memorandum-Funktion
Über die Aufzählungsfunktion (Python)
Python #Funktion 2 für Super-Anfänger
Nichtlineare Funktionsmodellierung in Python
Zeichne die Yin-Funktion in Python
Beispiel für Funktionen höherer Ordnung (Dekoratoren) in Python
Sofortige Funktion (Lüge) in Python
Relative URL-Verarbeitung in Python
[Python] Was ist eine Zip-Funktion?
[Python] Rückruffunktion (Funktion als Argument übergeben)
Implementieren Sie die Funktion power.prop.test von R in Python
Definition des Funktionsargumenttyps in Python
Inklusive Notation im Argument der Python-Funktion
[Mit Python automatisiert! ] Teil 1: Datei einstellen
Schreiben Sie die AWS Lambda-Funktion in Python
Messen Sie die Ausführungszeit von Funktionen in Python
[Python] Machen Sie die Funktion zu einer Lambda-Funktion
Memo zum Einstellen der Vim + Python-Entwicklungsumgebung
Python #len Funktion für Super-Anfänger
Funktionssynthese und Anwendung in Python
Verwendung der Zip-Funktion von Python
[Python] Unterschied zwischen Funktion und Methode
[Python] Funktionsargumente * (Stern) und ** (Doppelstern)
[OpenCV; Python] Zusammenfassung der Funktion findcontours