0

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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure what the ls -laR command is exactly but I have the ls output: Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 10/20/2021 11:37 PM landing d----- 10/20/2021 11:37 PM social d----- 10/20/2021 11:30 PM templates d----- 10/19/2021 11:17 AM VegaPlatform -a---- 10/20/2021 11:58 PM 143360 db.sqlite3 -a---- 10/19/2021 11:17 AM 690 manage.py
0
  • 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 add

    from 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') ),
    ]
          

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.