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分钟内接受答案