Reputation: 111
I have a project and an app. I am trying to use the urls.py in the project to activate a view held in the app.
The error message: import homepage.index ModuleNotFoundError: No module named 'homepage.index'
In the project urls.py I have this import statement:
from homepage.views import index
Then in the project's urls.py, the urlpatterns[] array includes this reference to the app's view:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', homepage.views.index, name='index'),
]
def index(request):
return HttpResponse("My Homepage")
Where might be the error?
Upvotes: 1
Views: 4465
Reputation: 4346
Django is a web framework that is written in python, there is no magic involved.You are specifying full path to your view in urls.py
# here the view is available as index
from homepage.views import index
# so reference the view as index
url(r'^$', index, name='index'),
if you need to reference will full namespace,
# here the view can be imported as you intented
import homepage
url(r'^$', homepage.views.index, name='index'),
Things to be noted
ModuleNotFound
is raised when the module refered is not available,
in this case python
will try to use homepage
module but it is not available in the current context.
ImportError
is raised when the refered attribute or module is not available in already imported module or when you are using from module import x
.
>>> from os import wow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'wow'
Upvotes: 0
Reputation: 2462
You are not importing correctly. Change
url(r'^$', homepage.views.index, name='index'),
to
url(r'^$', index, name='index'),
Upvotes: 2