简体   繁体   English

遵循使用 Django/Python 的教程:ModuleNotFoundError: No module named 'pages'

[英]Following a tutorial using Django/Python: ModuleNotFoundError: No module named 'pages'

I'm new to Django and am trying to create a very simple app off of a tutorial I found online.我是 Django 的新手,我正在尝试根据我在网上找到的教程创建一个非常简单的应用程序。

Working on a mac Django Version 2.0.7 Python 3.7.0在 Mac 上工作 Django 版本 2.0.7 Python 3.7.0

My file structure:我的文件结构:

helloworld
......venv
..........(other files)
......helloworld_project
..........(other files)
......manage.py
......pages
.........._ pycache _
..............otherfiles
..........admin.py
..........apps.py
..........migrations
..............(other files)
..........models.py
..........tests.py
..........urls.py
..........views.py

The problem: when I run my urls.py file, I get the following message:问题:当我运行我的 urls.py 文件时,我收到以下消息:

Traceback (most recent call last):


File "/Users/Bethany/Desktop/helloworld/pages/urls.py",         
line 3, in <module>
    from pages import views
ModuleNotFoundError: No module named 'pages'
My urls.py file:

# pages/urls.py
from django.urls import path
from pages import views

urlpatterns = [
    path('', views.homePageView, name='home')
]

I've tried replacing "from pages import views" with "from. import views" and get the same message.我尝试用“from.import views”替换“from pages import views”并得到相同的消息。

I've looked through a few similar questions on stack overflow, but haven't had success with finding a solution to fix my issue.我已经查看了一些关于堆栈溢出的类似问题,但没有成功找到解决我的问题的解决方案。 does anyone have any suggestions?有没有人有什么建议?

Thanks!谢谢!

If needed, this is the tutorial I'm following: https://djangoforbeginners.com/hello-world/如果需要,这是我正在关注的教程: https://djangoforbeginners.com/hello-world/

You are not importing views from page actually, you want to import the HomePageView from your views.您实际上不是从页面导入views ,而是希望从视图中导入 HomePageView。

Change your code to this:将您的代码更改为:

 # pages/urls.py
from django.urls import path
from .views import homePageView

urlpatterns = [
    path('', views.homePageView, name='home')
]

Are you sure that __init__.py file is present in pages app?您确定页面应用程序中存在__init__.py文件吗? If this file is not present in the pages folder, then any file will not be considered as a module.如果页面文件夹中不存在此文件,则任何文件都不会被视为模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Django/Python 创建初学者应用程序:ModuleNotFoundError: No module named &#39;pages&#39; - Creating a beginners app using Django/Python: ModuleNotFoundError: No module named 'pages' ModuleNotFoundError:没有名为“教程”的模块 - ModuleNotFoundError: No module named 'tutorial' Python 3.7.3错误:ModuleNotFoundError:没有名为&#39;Pages&#39;的模块 - Python 3.7.3 error: ModuleNotFoundError: No module named 'Pages' ModuleNotFoundError:测试时在 Django 教程中没有名为“mysite.polls”的模块 - ModuleNotFoundError: No module named 'mysite.polls' in django tutorial while testing "Python\/Django ModuleNotFoundError:没有名为“餐厅”的模块" - Python/Django ModuleNotFoundError: No module named 'restaurants' Python Django - ModuleNotFoundError:没有名为“picamera”的模块 - Python Django - ModuleNotFoundError: No module named 'picamera' Python新手:“ ModuleNotFoundError:没有名为&#39;django&#39;的模块” - New to Python: “ModuleNotFoundError: No module named 'django'” Django / Python:ModuleNotFoundError:没有名为“d”的模块 - Django / Python : ModuleNotFoundError: No module named 'd' ModuleNotFoundError: 没有名为 &#39;django 的模块 - ModuleNotFoundError: No module named 'django ModuleNotFoundError:没有名为“django”的模块 - ModuleNotFoundError: No module named 'django'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM