[英]Cannot import name 'views',.Python, Django
我在這個論壇上閱讀了很多答案,但它們並沒有解決我的問題。 我將非常感謝您的幫助。
我的文件 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)僅文件索引。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_import
或homepage import views
解決我的問題。 我還嘗試將views.py復制到目錄模板(並修改其代碼),但不幸的是它不起作用
您需要將views
和urls
分開,在您的應用程序中創建一個新模塊(文件) 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
]
路徑: 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 .views import index
可能有單獨的 urls 和 views.py 文件夾,你需要做的是你必須從 appname 導入視圖中編寫假設你的應用程序名稱是 myapp 你必須從 myapp 導入視圖中編寫
使用應該在天氣中創建新的 url.py 並在 views.py 中編寫代碼
網址.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'))
]
我有類似的問題,它沒有解決,直到我在 root 中添加了一個空的 views.py 例如,在你的情況下添加到 Environemnt\\the_weather\\the_weather
您需要將您的 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
from django.contrib import admin
from django.urls import path
import main.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', main.views.home_page, name="home_page"),
]
好吧,我也面臨同樣的問題。 我不斷收到服務器錯誤,但后來我發現這是一個簡單的錯誤。
go 到 urls.py 文件
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = index), #the path for our index view
]
我只需要刪除name = index
並且服務器出現 0 個錯誤。
是您的問題現在已經解決了嗎,我正在學習Python速成課程,並且遇到了相同的問題,如果您可以共享解決方案,我將不勝感激!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.