我正在关注django 2.1教程,基于此链接https://docs.djangoproject.com/en/2.1/topics/class-based-views/这是我的书籍代码/ urls.py
from django.urls import path, re_path
from . import views
from book.views import BookListView
app_name = 'book'
urlpatterns = [
path('', views.index, name = 'index')
path('list/', BookListView.as_view(template_name="media/templates/book/book_list.html")),
]
以下是我的书/ views.py
from django.shortcuts import render
from .models import Author, Book, BookInstance, Genre
from django.views.generic import ListView
def index(request):
num_books = Book.objects.all().count()
num_instances = BookInstance.objects.all().count()
num_instances_available = BookInstance.objects.filter(status__exact = 'a').count()
num_author = Author.objects.count()
context = {
'num_books' : num_books,
'num_instances' : num_instances,
'num_instances_available' : num_instances_available,
'num_author' : num_author,
}
return render(request, 'book/index.html', context)
class BookListView(ListView):
model = Book
我得到的错误是
文件“E:\ DJango \ mysite \ book \ urls.py”,第8行路径('list /',BookListView.as_view(template_name =“media / templates / book / book_list.html”)),^ SyntaxError:invalid句法
2 回答
存储html文件的标准方法是在Django应用程序下创建一个名为“templates”的文件夹 . 您还可以在templates文件夹中添加另一个与app相同名称的文件夹 .
这样你就可以像这样在你的_713509中调用这个文件:
如果您只创建了“templates”文件夹,则可以执行以下操作:
您也可以在基于类的视图中调用您正在使用的模板:
或者只有“templates”文件夹: