我剛開始一個django項目,但我不能從views.py導入一個函數。 我已經做了一些與這個框架非常相似的設置,但沒有任何問題的項目。 我正在使用virtualenv(它被激活),windows10,pycharm。在Django導入錯誤url.py
#urls.py
from django.conf.urls import url
from django.contrib import admin
from WebShop.main_app import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
]
#views.py
def index(request):
return HttpResponse("<h1>Hi</h1>")
#cmd
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\core\management\base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\env\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\b021l\Desktop\practice\django\WebShopProject\WebShop\WebShop\urls.py", line 19, in <module>
from WebShop.main_app import views
ImportError: No module named 'WebShop.main_app'
*編輯
我還添加了的virtualenv在pycharm項目解釋。
順便說一句我filestructure是基本的:
-WebShopProject
-env
-WebShop
-WebShop
-__init__.py
-settings.py
-urls.py .....
-main_app
-__init__.py
-views.py ...
**更新
仍然一無所獲。我重新安裝了蟒蛇,重新開工的項目,新的virtualenv,配置的virtualenv在pycharm默認的解釋,仍然沒什麼..試圖把urls.py在我的「main_app」文件夾和「WebShop.urls.py」包括它,你猜怎麼着,我不能從同一個文件夾導入..
*固定 我不知道發生了什麼它剛開始工作,也許它是windows的bug:/儘管感謝答案。
已包含該應用程序在你的'INSTALLED_APPS'在'settings.py'文件?您可能還想嘗試'從WebShop import views'或'from main_app import views',但這取決於您的文件結構。 – 1GDST
是的,它在安裝的應用程序中。這些這兩個進口都沒有工作..我只能輸入WebShop.main_app – Zozo
您是否已將'WebShop.main_app,'添加到您的'INSTALLED_APPS'或只是'main_app,'? – 1GDST