django 属性错误:模块"main.views"没有属性"home"

mbjcgjjk  于 2022-12-30  发布在  Go
关注(0)|答案(3)|浏览(131)

我正在做一个django项目,每当我运行“python manage.py runserver”时,我看到上面的错误。
views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import ToDoList, Item

# Create your views here.

def index(response, id):
    ls = ToDoList.objects.get(id=id)
    return render(response, "main/base.html", {})

def home(response):
    return render(response, "main/home.html", {})

url.py

from django.urls import path
from main import views
from . import views

urlpatterns = [
path("<int:id>", views.index, name="index"),
path("", views.home, name="home")
]

我的网站url.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("main.urls")),
]

谢谢你的帮助。

dgsult0t

dgsult0t1#

urls.py

from django.urls import path
from main import views
from . import views

最后两个导入都导入了名称views,因此第一个被第二个覆盖。

iyfamqjs

iyfamqjs2#

您的home函数缩进错误,它当前是index函数 * 内部 * 的函数,不在全局作用域中。
将其更改为以下内容
views.py

def index(response, id):
      ls = ToDoList.objects.get(id=id)
      return render(response, "main/base.html", {})

def home(response): # Don't indent this!
    return render(response, "main/home.html", {})
aemubtdh

aemubtdh3#

必须有一个配置问题,但如果你想看看网站是否工作或def主页():正在工作。请将代码更改为

def home(request):
return HttpResponse('<h1>Blog Home</h1>')

相关问题