VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • django学习笔记urls(1)

urls

urls中使用

1 urlpatterns = [
2     path('admin/', admin.site.urls),
3     path('polls/', include('polls.urls', namespace='polls')),
4 ]

运行后报错,错误代码如下:

1 django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name att
2 ribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

根据报错,我们可知,需要提供一个app_name,我们来看源码

复制代码
def include(arg, namespace=None):
    app_name = None
    if isinstance(arg, tuple):
        # Callable returning a namespace hint.
        try:
            urlconf_module, app_name = arg    # 从此处可以看出,需要提供一个二元元组,二元元组第二个参数需要提供app_name这个名称
        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)
复制代码
从第六行可以看出,需要提供一个二元元组,二元元组第二个参数需要提供app_name这个名称。因此,
方法一:修改urls.py为
urlpatterns = [
    path('admin/', admin.site.urls),
    path('polls/', include(('polls.urls', 'polls'), namespace='polls')),
]

方法二:在[app_name]目录下的urls.py中的urlpatterns前面加上app_name='[app_name]'

复制代码
from django.urls import path

from polls import views

app_name = 'polls'

urlpatterns = [
    path('', views.index, name="index"),
    path('<int:question_id>/', views.details, name="details"),
    path('<int:question_id>/results/', views.results, name="results"),
    path('<int:question_id>/vote/', views.vote, name="vote"),
]
复制代码

 

出处:https://www.cnblogs.com/paif/p/Pai.html

 


相关教程