在Django url.py中导入错误

时间:2016-10-22 18:20:19

标签: python django import django-views pycharm

我刚开始一个django项目,但我无法从views.py导入一个函数。 我已经完成了一些与此框架具有非常相似设置但没有任何问题的项目。 我使用virtualenv(并且已激活),windows10,pycharm。

#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中的项目解释器。

顺便说一下我的文件结构是基本的:

-WebShopProject
 -env
 -WebShop
  -WebShop
    -__init__.py
    -settings.py
    -urls.py .....
  -main_app
    -__init__.py
    -views.py ...

**更新

仍然没有..我重新安装了python,重新启动了项目,新的virtualenv,将virtualenv配置为pycharm中的默认解释器,仍然没有。试图将urls.py放在我的&#39; main_app&#39;文件夹并将其包含在&#39; WebShop.urls.py&#39;并猜测我不能从同一个文件夹导入..

*固定 我不知道发生了什么,它刚刚开始工作,也许它是Windows的错误:/虽然感谢答案。

1 个答案:

答案 0 :(得分:3)

您的文件结构中的

似乎需要这样做 from main_app import views