简体   繁体   中英

Creating a beginners app 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/

Since the urls file is already in the app pages you can't import it using the said name. I would suggest you change from pages import views to from . import views from . import views

since the urls.py and and views.py in the same directory therefor you can simply do from . import views from . import views it will import all the views.

几分钟前我遇到了同样的问题,但对我来说导致同样问题的唯一问题是在 settings.py 而不是“pages.apps.PagesConfig”中配置我的应用程序时我写的“pages.apps.pagesconfig”和然后问题经常很快得到解决。

The exact same thing happened with me today, turns out i was missing the comma after 'pages.apps.PageConfig'. Super silly mistake

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM