Specifying a namespace in include() without providing an app_name is not supported

2024-04-20 18:08

本文主要是介绍Specifying a namespace in include() without providing an app_name is not supported,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    在学习django 2.0开发web应用程序过程中,通过项目基础URL(http://localhost:8000/)返回默认的Django网站。如若我们需要房访问自己建立的网站,就需要通过URL映射自己程序的网页。

    通过添加自身程序代码来进行URL映射过程中,发现程序报错:

  File "F:\编程资料\python\python project\learndjango\learndjango\urls.py", line 21, in <module>path('', include('learndjangos.urls', namespace='learndjangos'))File "E:\program files\Python\Python36\lib\site-packages\django\urls\conf.py", line 39, in include'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.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.

    查看django.urls中include函数源码:

"""Functions for use in URLsconfs."""
from functools import partial
from importlib import import_modulefrom django.core.exceptions import ImproperlyConfiguredfrom .resolvers import (LocalePrefixPattern, RegexPattern, RoutePattern, URLPattern, URLResolver,
)def include(arg, namespace=None):app_name = Noneif isinstance(arg, tuple):# Callable returning a namespace hint.try:urlconf_module, app_name = argexcept 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 = argif 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)def _path(route, view, kwargs=None, name=None, Pattern=None):if isinstance(view, (list, tuple)):# For include(...) processing.pattern = Pattern(route, is_endpoint=False)urlconf_module, app_name, namespace = viewreturn URLResolver(pattern,urlconf_module,kwargs,app_name=app_name,namespace=namespace,)elif callable(view):pattern = Pattern(route, name=name, is_endpoint=True)return URLPattern(pattern, view, kwargs, name)else:raise TypeError('view must be a callable or a list/tuple in the case of include().')path = partial(_path, Pattern=RoutePattern)
re_path = partial(_path, Pattern=RegexPattern)

    发现在Django 2.0中,django.urls中include函数第一个参数传入的是tuple类型,其中一个是参数是app_name,且app_name需赋值。我们修改程序:

from django.contrib import admin
from django.urls import path, includeurlpatterns = [path('admin/', admin.site.urls),path(r'', include(('learndjangos.urls', 'learndjangos'), namespace='learndjangos'))
]

    在views.py中代码如下:

from django.shortcuts import render# Create your views here.def index(request):return render(request, 'learndjangos/index.html')

    经过测试,访问http://localhost:8000,页面顺利打开。

这篇关于Specifying a namespace in include() without providing an app_name is not supported的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/920965

相关文章

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

macOS怎么轻松更换App图标? Mac电脑图标更换指南

《macOS怎么轻松更换App图标?Mac电脑图标更换指南》想要给你的Mac电脑按照自己的喜好来更换App图标?其实非常简单,只需要两步就能搞定,下面我来详细讲解一下... 虽然 MACOS 的个性化定制选项已经「缩水」,不如早期版本那么丰富,www.chinasem.cn但我们仍然可以按照自己的喜好来更换

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

Unable to instantiate Action, goodsTypeAction, defined for 'goodsType_findAdvanced' in namespace '/

报错: Unable to instantiate Action, goodsTypeAction,  defined for 'goodsType_findAdvanced' in namespace '/'goodsTypeAction......... Caused by: java.lang.ClassNotFoundException: goodsTypeAction.......

MFC中App,Doc,MainFrame,View各指针的互相获取

纸上得来终觉浅,为了熟悉获取方法,我建了个SDI。 首先说明这四个类的执行顺序是App->Doc->Main->View 另外添加CDialog类获得各个指针的方法。 多文档的获取有点小区别,有时间也总结一下。 //  App void CSDIApp::OnApp() {      //  App      //  Doc     CDocument *pD

ConstraintLayout布局里的一个属性app:layout_constraintDimensionRatio

ConstraintLayout 这是一个约束布局,可以尽可能的减少布局的嵌套。有一个属性特别好用,可以用来动态限制宽或者高app:layout_constraintDimensionRatio 关于app:layout_constraintDimensionRatio参数 app:layout_constraintDimensionRatio=“h,1:1” 表示高度height是动态变化

App Store最低版本要求汇总

1,自此日期起: 2024 年 4 月 29 日 自 2024 年 4 月 29 日起,上传到 App Store Connect 的 App 必须是使用 Xcode 15 为 iOS 17、iPadOS 17、Apple tvOS 17 或 watchOS 10 构建的 App。将 iOS App 提交至 App Store - Apple Developer 2,最低XCode版本 Xcod