Python django.conf.urls() Examples

The following are 20 code examples for showing how to use django.conf.urls(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.conf , or try the search function .

Example 1
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: resolvers.py    License: MIT License 6 votes vote down vote up
def _check_pattern_startswith_slash(self):
        """
        Check that the pattern does not begin with a forward slash.
        """
        regex_pattern = self.regex.pattern
        if not settings.APPEND_SLASH:
            # Skip check as it can be useful to start a URL pattern with a slash
            # when APPEND_SLASH=False.
            return []
        if regex_pattern.startswith(('/', '^/', '^\\/')) and not regex_pattern.endswith('/'):
            warning = Warning(
                "Your URL pattern {} has a route beginning with a '/'. Remove this "
                "slash as it is unnecessary. If this pattern is targeted in an "
                "include(), ensure the include() pattern has a trailing '/'.".format(
                    self.describe()
                ),
                id="urls.W002",
            )
            return [warning]
        else:
            return [] 
Example 2
Project: python   Author: Yeah-Kun   File: resolvers.py    License: Apache License 2.0 6 votes vote down vote up
def _check_pattern_startswith_slash(self):
        """
        Check that the pattern does not begin with a forward slash.
        """
        regex_pattern = self.regex.pattern
        if not settings.APPEND_SLASH:
            # Skip check as it can be useful to start a URL pattern with a slash
            # when APPEND_SLASH=False.
            return []
        if (regex_pattern.startswith('/') or regex_pattern.startswith('^/')) and not regex_pattern.endswith('/'):
            warning = Warning(
                "Your URL pattern {} has a regex beginning with a '/'. Remove this "
                "slash as it is unnecessary. If this pattern is targeted in an "
                "include(), ensure the include() pattern has a trailing '/'.".format(
                    self.describe()
                ),
                id="urls.W002",
            )
            return [warning]
        else:
            return [] 
Example 3
Project: python2017   Author: bpgc-cte   File: resolvers.py    License: MIT License 6 votes vote down vote up
def _check_pattern_startswith_slash(self):
        """
        Check that the pattern does not begin with a forward slash.
        """
        regex_pattern = self.regex.pattern
        if not settings.APPEND_SLASH:
            # Skip check as it can be useful to start a URL pattern with a slash
            # when APPEND_SLASH=False.
            return []
        if (regex_pattern.startswith('/') or regex_pattern.startswith('^/')) and not regex_pattern.endswith('/'):
            warning = Warning(
                "Your URL pattern {} has a regex beginning with a '/'. Remove this "
                "slash as it is unnecessary. If this pattern is targeted in an "
                "include(), ensure the include() pattern has a trailing '/'.".format(
                    self.describe()
                ),
                id="urls.W002",
            )
            return [warning]
        else:
            return [] 
Example 4
Project: GTDWeb   Author: lanbing510   File: urlresolvers.py    License: GNU General Public License v2.0 5 votes vote down vote up
def resolve_error_handler(self, view_type):
        callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
        if not callback:
            # No handler specified in file; use default
            # Lazy import, since django.urls imports this file
            from django.conf import urls
            callback = getattr(urls, 'handler%s' % view_type)
        return get_callable(callback), {} 
Example 5
Project: bioforum   Author: reBiocoder   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _check_pattern_startswith_slash(self):
        """
        Check that the pattern does not begin with a forward slash.
        """
        regex_pattern = self.regex.pattern
        if not settings.APPEND_SLASH:
            # Skip check as it can be useful to start a URL pattern with a slash
            # when APPEND_SLASH=False.
            return []
        if regex_pattern.startswith(('/', '^/', '^\\/')) and not regex_pattern.endswith('/'):
            warning = Warning(
                "Your URL pattern {} has a route beginning with a '/'. Remove this "
                "slash as it is unnecessary. If this pattern is targeted in an "
                "include(), ensure the include() pattern has a trailing '/'.".format(
                    self.describe()
                ),
                id="urls.W002",
            )
            return [warning]
        else:
            return [] 
Example 6
Project: bioforum   Author: reBiocoder   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _check_include_trailing_dollar(self):
        regex_pattern = self.regex.pattern
        if regex_pattern.endswith('$') and not regex_pattern.endswith(r'\$'):
            return [Warning(
                "Your URL pattern {} uses include with a route ending with a '$'. "
                "Remove the dollar from the route to avoid problems including "
                "URLs.".format(self.describe()),
                id='urls.W001',
            )]
        else:
            return [] 
Example 7
Project: bioforum   Author: reBiocoder   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _route_to_regex(route, is_endpoint=False):
    """
    Convert a path pattern into a regular expression. Return the regular
    expression and a dictionary mapping the capture names to the converters.
    For example, 'foo/<int:pk>' returns '^foo\\/(?P<pk>[0-9]+)'
    and {'pk': <django.urls.converters.IntConverter>}.
    """
    original_route = route
    parts = ['^']
    converters = {}
    while True:
        match = _PATH_PARAMETER_COMPONENT_RE.search(route)
        if not match:
            parts.append(re.escape(route))
            break
        parts.append(re.escape(route[:match.start()]))
        route = route[match.end():]
        parameter = match.group('parameter')
        if not parameter.isidentifier():
            raise ImproperlyConfigured(
                "URL route '%s' uses parameter name %r which isn't a valid "
                "Python identifier." % (original_route, parameter)
            )
        raw_converter = match.group('converter')
        if raw_converter is None:
            # If a converter isn't specified, the default is `str`.
            raw_converter = 'str'
        try:
            converter = get_converter(raw_converter)
        except KeyError as e:
            raise ImproperlyConfigured(
                "URL route '%s' uses invalid converter %s." % (original_route, e)
            )
        converters[parameter] = converter
        parts.append('(?P<' + parameter + '>' + converter.regex + ')')
    if is_endpoint:
        parts.append('$')
    return ''.join(parts), converters 
Example 8
Project: bioforum   Author: reBiocoder   File: resolvers.py    License: MIT License 5 votes vote down vote up
def check(self):
        warnings = self._check_pattern_startswith_slash()
        route = self._route
        if '(?P<' in route or route.startswith('^') or route.endswith('$'):
            warnings.append(Warning(
                "Your URL pattern {} has a route that contains '(?P<', begins "
                "with a '^', or ends with a '$'. This was likely an oversight "
                "when migrating to django.urls.path().".format(self.describe()),
                id='2_0.W001',
            ))
        return warnings 
Example 9
Project: bioforum   Author: reBiocoder   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _check_pattern_name(self):
        """
        Check that the pattern name does not contain a colon.
        """
        if self.pattern.name is not None and ":" in self.pattern.name:
            warning = Warning(
                "Your URL pattern {} has a name including a ':'. Remove the colon, to "
                "avoid ambiguous namespace references.".format(self.pattern.describe()),
                id="urls.W003",
            )
            return [warning]
        else:
            return [] 
Example 10
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _check_include_trailing_dollar(self):
        regex_pattern = self.regex.pattern
        if regex_pattern.endswith('$') and not regex_pattern.endswith(r'\$'):
            return [Warning(
                "Your URL pattern {} uses include with a route ending with a '$'. "
                "Remove the dollar from the route to avoid problems including "
                "URLs.".format(self.describe()),
                id='urls.W001',
            )]
        else:
            return [] 
Example 11
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _route_to_regex(route, is_endpoint=False):
    """
    Convert a path pattern into a regular expression. Return the regular
    expression and a dictionary mapping the capture names to the converters.
    For example, 'foo/<int:pk>' returns '^foo\\/(?P<pk>[0-9]+)'
    and {'pk': <django.urls.converters.IntConverter>}.
    """
    original_route = route
    parts = ['^']
    converters = {}
    while True:
        match = _PATH_PARAMETER_COMPONENT_RE.search(route)
        if not match:
            parts.append(re.escape(route))
            break
        parts.append(re.escape(route[:match.start()]))
        route = route[match.end():]
        parameter = match.group('parameter')
        if not parameter.isidentifier():
            raise ImproperlyConfigured(
                "URL route '%s' uses parameter name %r which isn't a valid "
                "Python identifier." % (original_route, parameter)
            )
        raw_converter = match.group('converter')
        if raw_converter is None:
            # If a converter isn't specified, the default is `str`.
            raw_converter = 'str'
        try:
            converter = get_converter(raw_converter)
        except KeyError as e:
            raise ImproperlyConfigured(
                "URL route '%s' uses invalid converter %s." % (original_route, e)
            )
        converters[parameter] = converter
        parts.append('(?P<' + parameter + '>' + converter.regex + ')')
    if is_endpoint:
        parts.append('$')
    return ''.join(parts), converters 
Example 12
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: resolvers.py    License: MIT License 5 votes vote down vote up
def check(self):
        warnings = self._check_pattern_startswith_slash()
        route = self._route
        if '(?P<' in route or route.startswith('^') or route.endswith('$'):
            warnings.append(Warning(
                "Your URL pattern {} has a route that contains '(?P<', begins "
                "with a '^', or ends with a '$'. This was likely an oversight "
                "when migrating to django.urls.path().".format(self.describe()),
                id='2_0.W001',
            ))
        return warnings 
Example 13
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _check_pattern_name(self):
        """
        Check that the pattern name does not contain a colon.
        """
        if self.pattern.name is not None and ":" in self.pattern.name:
            warning = Warning(
                "Your URL pattern {} has a name including a ':'. Remove the colon, to "
                "avoid ambiguous namespace references.".format(self.pattern.describe()),
                id="urls.W003",
            )
            return [warning]
        else:
            return [] 
Example 14
Project: python   Author: Yeah-Kun   File: resolvers.py    License: Apache License 2.0 5 votes vote down vote up
def _check_pattern_name(self):
        """
        Check that the pattern name does not contain a colon.
        """
        if self.name is not None and ":" in self.name:
            warning = Warning(
                "Your URL pattern {} has a name including a ':'. Remove the colon, to "
                "avoid ambiguous namespace references.".format(self.describe()),
                id="urls.W003",
            )
            return [warning]
        else:
            return [] 
Example 15
Project: python   Author: Yeah-Kun   File: resolvers.py    License: Apache License 2.0 5 votes vote down vote up
def _check_include_trailing_dollar(self):
        """
        Check that include is not used with a regex ending with a dollar.
        """
        regex_pattern = self.regex.pattern
        if regex_pattern.endswith('$') and not regex_pattern.endswith(r'\$'):
            warning = Warning(
                "Your URL pattern {} uses include with a regex ending with a '$'. "
                "Remove the dollar from the regex to avoid problems including "
                "URLs.".format(self.describe()),
                id="urls.W001",
            )
            return [warning]
        else:
            return [] 
Example 16
Project: python   Author: Yeah-Kun   File: resolvers.py    License: Apache License 2.0 5 votes vote down vote up
def resolve_error_handler(self, view_type):
        callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
        if not callback:
            # No handler specified in file; use lazy import, since
            # django.conf.urls imports this file.
            from django.conf import urls
            callback = getattr(urls, 'handler%s' % view_type)
        return get_callable(callback), {} 
Example 17
Project: luscan-devel   Author: blackye   File: urlresolvers.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _resolve_special(self, view_type):
        callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
        if not callback:
            # No handler specified in file; use default
            # Lazy import, since django.urls imports this file
            from django.conf import urls
            callback = getattr(urls, 'handler%s' % view_type)
        return get_callable(callback), {} 
Example 18
Project: python2017   Author: bpgc-cte   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _check_pattern_name(self):
        """
        Check that the pattern name does not contain a colon.
        """
        if self.name is not None and ":" in self.name:
            warning = Warning(
                "Your URL pattern {} has a name including a ':'. Remove the colon, to "
                "avoid ambiguous namespace references.".format(self.describe()),
                id="urls.W003",
            )
            return [warning]
        else:
            return [] 
Example 19
Project: python2017   Author: bpgc-cte   File: resolvers.py    License: MIT License 5 votes vote down vote up
def _check_include_trailing_dollar(self):
        """
        Check that include is not used with a regex ending with a dollar.
        """
        regex_pattern = self.regex.pattern
        if regex_pattern.endswith('$') and not regex_pattern.endswith(r'\$'):
            warning = Warning(
                "Your URL pattern {} uses include with a regex ending with a '$'. "
                "Remove the dollar from the regex to avoid problems including "
                "URLs.".format(self.describe()),
                id="urls.W001",
            )
            return [warning]
        else:
            return [] 
Example 20
Project: python2017   Author: bpgc-cte   File: resolvers.py    License: MIT License 5 votes vote down vote up
def resolve_error_handler(self, view_type):
        callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
        if not callback:
            # No handler specified in file; use lazy import, since
            # django.conf.urls imports this file.
            from django.conf import urls
            callback = getattr(urls, 'handler%s' % view_type)
        return get_callable(callback), {}