ModuleNotFoundError: No module named 'view'

Question:

My filetree looks like this:

So in my urls.py folder, I have the following code:


from django.urls import path
from view import views
#URLConf
urlpatterns = [
    path('hello/', views.say_hello)

]

The error I get when I am trying to python manage.py runserver is

File "/Users/swathikumar/Desktop/storefront/playground/urls.py", line 3, in
from view import views
ModuleNotFoundError: No module named ‘view’

View is a folder I created which has both init.py file and views.py file which has the following code:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
# request -> response
# request handler
# action
def say_hello(request):
    return HttpResponse('Hello World.')

I do not know how to solve this error of resolving the import of views into urls.py. All these files are in a playground app folder.

I was expecting my serevr to give me Hello World upon entering the http request.

Asked By: Swathi Kumar

||

Answers:

The name of the app is playground, if I read that correctly, so you import this with:

#       🖟 name of the app
from playground import views
Answered By: willeM_ Van Onsem

Use below code.

from django.urls import path
from . import views
#URLConf
urlpatterns = [
    path('hello/', views.say_hello)

]


Answered By: Pycm

You need to import the views from the current folder so you can access the current folder with dot(.) For example:

from . import models

from . import views

You can try this code

from django.urls import path
from . import views

#URLConf
urlpatterns = [
    path('hello/', views.say_hello)
]
Answered By: Aditya

Since view is a sub-module in the current module playground, you can import it as follows:

from django.urls import path
from .view import views # added dot in front of module to indicate current directory.
# URLConf
urlpatterns = [
    path('hello/', views.say_hello)

]

Or better yet

from django.urls import path
from playground.view import views
# URLConf
urlpatterns = [
    path('hello/', views.say_hello)

]
Answered By: Chukwujiobi Canon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.