【问题标题】:Cannot import name 'views',.Python, Django无法导入名称“视图”、.Python、Django
【发布时间】:2018-12-21 23:20:39
【问题描述】:

我在这个论坛上阅读了很多答案,但它们并没有解决我的问题。我将非常感谢您的帮助。

我的文件views.py返回这个错误:

from . import views
ImportError: cannot import name 'views' from '__main__' (C:/Users/tymot/Desktop/weather app/env/Environemnt/the_weather/weather/views.py)

views.py (Environemnt\the_weather\weather)

from django.shortcuts import render
from django.contrib import admin

def index(request):
    return render(request, 'weather/index.html') #returns the index.html 

urls.py (Environemnt\the_weather\weather)

from django.urls import path
from . import views

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

urls.py(环境\the_weather\the_weather)

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('weather.urls')),

模板(the_weather\weather\templates\weather) 只有文件 index.html

目录

-the_weather
--the_weather
---__init__
---setting
---urls
---wsgi
--weather
---migrations
----__init__
---templates
----weather
-----index
---__init__
---admin
---apps
---models
---tests
---urls
---views
--db
--manage.py

我尝试使用from __future__ import absolute_importhomepage import views 解决我的问题。我还尝试将views.py复制到目录模板(并修改其代码),但不幸的是它不起作用

【问题讨论】:

  • 你把urlpatterns写成views.py
  • 你用的是什么版本的 Python?
  • Python 3.7 Django 1.X

标签: python django


【解决方案1】:

您需要将viewsurls 分开,在您的应用程序中创建一个新模块(文件)urls.py,在您的情况下为weather 文件夹,并在其中添加这些代码,然后将其从@ 中删除987654326@,您可以阅读here了解它以更好地了解它。

路径the_weather/weather/urls.py

from django.urls import path
from . import views

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

路径the_weather/weather/views.py

from django.shortcuts import render
from django.contrib import admin

def index(request):
    return render(request, 'weather/index.html') #returns the index.html template

【讨论】:

  • 我现在的视图:from django.shortcuts import render from django.contrib import admin def index(request): return render(request, 'weather/index.html') 我现在天气中的 urls.py:from django.urls import path from . import views urlpatterns = [ path('', views.index), #the path for our index view 但不幸的是它仍然不起作用。
  • 我更新我的答案试试看,我写了filepath,你需要把你的代码放在哪里
  • 但是我现在在我的 urls.py 文件中看到了这些 ImportError: cannot import name 'views' from '__main__'
  • 你能编辑你的答案,把所有traceback放在那里吗?
  • 尝试使用from weather import views
【解决方案2】:

你为什么不这样尝试

from .views import index

【讨论】:

  • 我尝试在我的 url.py 中使用它。 PyCharm 的回答是No module named '__main__.views'; '__main__' is not a package
  • 能否分享您的代码和错误回溯的屏幕截图...都很重要..
【解决方案3】:

可能有单独的 urls 和 views.py 文件夹 你要做的是你必须写 从 appname 导入视图 假设您的应用名称是 myapp 你必须写 从 myapp 导入视图

【讨论】:

    【解决方案4】:

    使用应该在weather中创建新的url.py并在views.py中编写代码

    url.py

    from django.urls import path
    from . import views
    urlpatterns = [
       path('', views.index, name='index')
    ]
    

    在项目的 urls.py 中编写代码

    from django.urls import path, include
    from . import views
    urlpatterns = [
       path('wheather/',include('wheather'))
    ]
    

    【讨论】:

      【解决方案5】:

      我有类似的问题,它没有解决,直到我在示例中添加了一个空的 views.py 在根目录中,在你的情况下添加到 Environemnt\the_weather\the_weather

      【讨论】:

        【解决方案6】:

        您需要将views.py 文件放在天气文件夹中。确保它在正确的天气文件夹中,我猜你有两个天气文件夹。还要确保这些代码是正确的;

        #Path : the_weather/weather/urls.py

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

        #Path : the_weather/weather/views.py

        from django.shortcuts import render
        from django.contrib import admin
        
        def index(request):
            return render(request, 'weather/index.html') #returns the index.html template
        

        【讨论】:

          【解决方案7】:

          我遇到了同样的问题。

          formsdemo\formsdemo\__init__.py,我把导入代码改成了from forms app import views

          【讨论】:

          • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
          猜你喜欢
          • 2017-08-04
          • 2023-03-19
          • 1970-01-01
          • 2017-12-11
          • 2014-11-15
          • 2016-01-07
          • 2011-07-11
          • 2015-03-01
          • 2011-12-27
          相关资源
          最近更新 更多