I am trying to run a webserver in Django using this tutorial series: https://www.youtube.com/watch?v=Rpi0Ne1nMdk&list=PLPSM8rIid1a3TkwEmHyDALNuHhqiUiU5A
I get an error when I try and import the script 'landing.views' inside urls.py, here is the error in it's entirety:
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 224, in create
import_module(entry)
File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'views'
Here is the code that generates the error seen above in urls.py:
from django.urls import path
from landing.views import Index
urlpatterns = [
path('', Index.as_view(), name='index'),
]
I have also tried looking at posts that other users have created like these: ImportError: cannot import name 'Index' from 'landing.views', Django error in urlpatterns: No module named views, Cannot import name views, however, the first link is currently not answered, and the latter few are not really related to the code that I am currently using. I have also tried other methods like using
sys.path.insert(1, 'C:/Users/trevo/OneDrive/Desktop/VegaAiTest/ChattingPlatform/src/VegaPlatform/landing/urls.py')
and sys.path.append("C:/Users/trevo/OneDrive/Desktop/VegaAiTest/ChattingPlatform/src/VegaPlatform/landing/urls.py")
as well as using the SET PYTHONPATH=
command in my powershell application. What exactly am I doing wrong that might be causing these errors? Any help would be much appreciated, thank you!
CodePudding user response:
Move views.py
from project directory to application directory. If you don't understand the difference, show me ls -laR
output.
Have you renamed site from landing
to something else? Update from landing.views import Index
if so.
CodePudding user response:
The problem is when you leave path empty by leaving single quotes without a path, Django will search for views in the project directory.
Else, in urlpatterns
, you should always prefix the views path with views.
such views.index
So to fix it, there is 3 ways:
Solution 1
import landing.views urlpatterns = [ path('', landing.views.Index.as_view(), name='index'), ]
Solution 2
from landing.views import Index urlpatterns = [ path('index/', views.Index.as_view(),name='index'), ]
Solution 3: Inside landing app create
urls.py
an addfrom views import Index urlpatterns = [ path('index/', views.Index.as_view(),name='index') ]
and in project/urls.py
add
from django.urls import include, path
urlpatterns = [
path('landing/', include('landing.urls') ),
]