ImportError:无法从“ __main__”导入“视图”

时间:2020-05-30 12:55:55

标签: python django

这是我的网站(项目)文件上的民意调查应用程序中的url.py文件:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),   
]
File "<string>", line 2, in <module>
ImportError: cannot import name 'views' from '__main__' (/storage/emulated/0/website/polls/urls.py)

现在将网站文件中的主要urls.py文件

from django.contrib import admin

from django.urls import include, path

    urlpatterns = [
        path('polls/', include('polls.urls')),

        path('admin/', admin.site.urls),
    ]

跟踪代码:

Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>    
start(fakepyfile,mainpyfile)
File 

    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

[Program finished]

我已经这样尝试过民意调查urls.py 从民意调查导入视图中

但是它收到模块错误,那么代码有什么问题?

1 个答案:

答案 0 :(得分:0)

您需要将视图和url分开,在您的应用中创建一个新模块(文件)urls.py(如果您是weather文件夹),然后在其中添加这些代码,并将其从views.py中删除,您可以阅读在这里了解它,以便更好地了解它。

`路径:

the_weather / weather / urls.py`

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),  #the path for our index view
]