包含未定义 [英] Include is not defined

查看:25
本文介绍了包含未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了说明安全性,项目文件夹是文件夹",应用程序是帐户".我确定这是我忽略的一个基本问题,但是由于以下错误,我一直在努力使manged.py命令起作用:

To clarify Security is the project folder, and Accounts is the app. I'm sure this is an elementary issue that i'm overlooking, but i've been struggling to get my manged.py commands to work because the following error:

NameError:未定义名称"include".

NameError: name 'include' is not defined.

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

My project (Security) urls.py is the following, which does have include:

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包含:

My Accounts apps.py contains:

from django.apps import AppConfig


    class AccountsConfig(AppConfig):
        name = 'accounts'

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

My Accounts app urls.py is the following:

      from django.conf.urls import url, include

from . import views

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

我的settings.py在已安装的应用中拥有帐户:

My settings.py has accounts in the installed apps:

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

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

After adding include to be in both urls.py for my project and app, i'm now greeted with the following stating accounts module is not found.

    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'

推荐答案

您正在未导入文件的文件中使用 include 函数.您应该添加

You are using the include function in a file in which you're not importing it. You should add

from django.conf.urls import url, include

到您的安全应用urls.py

to your security app urls.py

编辑

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

For the second error, try and change the way you include the urls from accounts. Replace accounts.url for it's relative path, starting with the apps folder. For example

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

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

If you have doubts, add your folder structure to the question and I will update the answer properly.

这篇关于包含未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆