Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

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.

Working on a mac Django Version 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:

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.

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/

question from:https://stackoverflow.com/questions/65861273/following-a-tutorial-using-django-python-modulenotfounderror-no-module-named

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are not importing views from page actually, you want to import the HomePageView from your views.

Change your code to this:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...