包含未定义

时间:2017-09-11 14:57:16

标签: python django

澄清安全性是项目文件夹,而“帐户”是应用程序。我确定这是一个我忽略的基本问题,但我一直在努力让我的manged.py命令工作,因为以下错误:

NameError:未定义名称“include”。

我的项目(安全)urls.py如下,其中包括:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^account/', include('accounts.url')),
]

我的帐户apps.py包含:

from django.apps import AppConfig


    class AccountsConfig(AppConfig):
        name = 'accounts'

我的帐户应用urls.py如下:

      from django.conf.urls import url, include

from . import views

urlpatterns = [
   url(r'^$', views.home, name='home'),
]

我的settings.py在已安装的应用中包含帐户:

INSTALLED_APPS = [
    'accounts',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

在我的项目和应用程序的urls.py中添加include之后,我现在接受以下说明帐户模块未找到。

    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'accounts.url'

5 个答案:

答案 0 :(得分:1)

您在未导入文件的文件中使用include功能。你应该添加

from django.conf.urls import url, include

到您的安全应用urls.py

修改

对于第二个错误,请尝试更改您在帐户中添加网址的方式。从apps文件夹开始,将accounts.url替换为相对路径。例如

url(r'^account/', include('accounts.urls')),

如果您有疑问,请在问题中添加文件夹结构,我会正确更新答案。

答案 1 :(得分:0)

In your rootapp.py you have used
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/', include('accounts.url')),
]
Here you have used include('accounts.url')
But you have not imported include 
So your code should be

from django.apps import AppConfig


class AccountsConfig(AppConfig):
    name = 'accounts'

my security app urls.py is the following:

from django.conf.urls import url , include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^account/', include('accounts.url')),
]

答案 2 :(得分:0)

这是解决方案:

from django.conf.urls import include

将其导入项目的urls.py页上。

答案 3 :(得分:0)

嗨,我不确定,但是对我有用。我刚刚安装了包含此命令的模块(在虚拟环境中):

pip install include

该脚本不再显示错误。

答案 4 :(得分:0)

首先尝试“ pip install include”

您忘记保存views.py和urls.py

将此添加到您的urls.py中(从django.conf.urls导入包含中)