I am working on a Django app that will host a simple index.html file:
url.py:
from django.conf.urls import url,include
from django.contrib import admin
from PyDemo.accounts.views import my_view
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^demo/',my_view)
]
views.py:
from django.shortcuts import render_to_response
from django.template import RequestContext
# Create your views here.
def my_view(request):
return render_to_response('index.html',locals(),context_instance=RequestContext(request))
However, when I run the app, I get the following error:
return import_module(self.urlconf_name)
File "C:\Users\tcssuoy\Desktop\DJANGO\mysite\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\tcssuoy\workspace\PyDemo\PyDemo\PyDemo\urls.py", line 18, in <module>
from PyDemo.accounts.views import my_view
ModuleNotFoundError: No module named 'PyDemo.accounts'
It seems like your project name is PyDemo and the name of the module is accounts. So try using
from accounts.views import my_view
You may find an error (underlined by red mark) that is because of your IDE so ignore it and migrate it. It will work
For anyone else with this issue who is migrating from Python 2 to 3 it seems to be because Python 3 is more discerning about where to find modules. @Ranjith Singhu's comment sent me in the right direction. In my case I had to change:
Python 2:
from views import MyView
Python 3 (also fine in 2):
from project_name.views import MyView