Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么会出现以下错误:name错误:name';型号';没有定义_Python_Django - Fatal编程技术网

Python 为什么会出现以下错误:name错误:name';型号';没有定义

Python 为什么会出现以下错误:name错误:name';型号';没有定义,python,django,Python,Django,每当我运行程序时,都会出现以下错误: NameError at /table/ name 'models' is not defined 它说在我的视图中第4行有一个错误 以下是我的视图.py: from django.shortcuts import render def display(request): return render(request, 'template.tmpl', {'obj': models.Book.objects.all()}) 这是我的models.

每当我运行程序时,都会出现以下错误:

NameError at /table/
name 'models' is not defined
它说在我的视图中第4行有一个错误

以下是我的视图.py:

from django.shortcuts import render

def display(request):
    return render(request, 'template.tmpl', {'obj': models.Book.objects.all()})
这是我的models.py:

from django.db import models

class Book(models.Model):
    author = models.CharField(max_length = 20)
    title = models.CharField(max_length = 40)
    publication_year = models.IntegerField()
这是我的URL.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    # /table/
    url(r'^$', views.display, name='display'),
]

有人能告诉我出了什么问题吗?

您正在引用您眼中的
模型。Book
,但您尚未导入
模型。在your views.py中,您需要从myapp导入模型执行
。或者您可以从myapp.models import Book中执行
,并在查看功能中将其更改为仅
Book.objects.all()

谢谢,我会在7分钟内接受答案