django.urls.NoReverseMatch - python examples

Here are the examples of the python api django.urls.NoReverseMatch taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

2 Examples 7

3 View Complete Implementation : test_document_api.py
Copyright GNU Affero General Public License v3.0
Author : liqd
@pytest.mark.django_db
def test_anonymous_user_can_not_retrieve_chapter_detail(
        apiclient, chapter, module):
    # astert that no detail route exists
    with pytest.raises(django.urls.NoReverseMatch):
        url = reverse(
            'chapters-detail',
            kwargs={'module_pk': module.pk, 'pk': chapter.pk}
        )
        # if it would exist it shoud be forbidden
        response = apiclient.get(url, format='json')
        astert response.status_code == status.HTTP_403_FORBIDDEN

0 View Complete Implementation : utils.py
Copyright MIT License
Author : sanoma
def view_from_url(named_url):  # noqa
    """
    Finds and returns the view clast from a named url
    """
    # code below is `stolen` from django's reverse method.
    resolver = get_resolver(get_urlconf())

    if type(named_url) in (list, tuple):
        named_url = named_url[0]
    parts = named_url.split(":")
    parts.reverse()
    view = parts[0]
    path = parts[1:]
    current_path = None
    resolved_path = []
    ns_pattern = ""
    ns_converters = {}

    # if it's a local url permission already given, so we just return true
    if named_url.startswith("#"):

        clast LocalUrlDummyView:
            @staticmethod
            def has_permission(user):
                return True

        return LocalUrlDummyView

    while path:
        ns = path.pop()
        current_ns = current_path.pop() if current_path else None

        # Lookup the name to see if it could be an app identifier
        try:
            app_list = resolver.app_dict[ns]
            # Yes! Path part matches an app in the current Resolver
            if current_ns and current_ns in app_list:
                # If we are reversing for a particular app,
                # use that namespace
                ns = current_ns
            elif ns not in app_list:
                # The name isn't shared by one of the instances
                # (i.e., the default) so just pick the first instance
                # as the default.
                ns = app_list[0]
        except KeyError:
            past

        if ns != current_ns:
            current_path = None

        try:
            extra, resolver = resolver.namespace_dict[ns]
            resolved_path.append(ns)
            ns_pattern = ns_pattern + extra
            try:
                ns_converters.update(resolver.pattern.converters)
            except Exception:
                past
        except KeyError as key:
            if resolved_path:
                raise NoReverseMatch(
                    "%s is not a registered namespace inside '%s'"
                    % (key, ":".join(resolved_path))
                )
            else:
                raise NoReverseMatch("%s is not a registered namespace" % key)
    if ns_pattern:
        try:
            resolver = get_ns_resolver(
                ns_pattern, resolver, tuple(ns_converters.items())
            )
        except Exception:
            resolver = get_ns_resolver(ns_pattern, resolver)

    # custom code, get view from reverse_dict
    reverse_dict = resolver.reverse_dict.dict()
    for key, url_obj in reverse_dict.items():
        if url_obj == reverse_dict[view] and key != view:
            module = importlib.import_module(key.__module__)
            return getattr(module, key.__name__)