python - 在 /catalog/borrowed/ 配置不当
问题描述
我是 python-django 编码的初学者。目前我正在尝试完成https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Authentication上的挑战,但我面临
LoanedBooksByAllUserListView 缺少 permission_required 属性。定义 LoanedBooksByAllUserListView.permission_required,或覆盖 LoanedBooksByAllUserListView.get_permission_required()。
在 urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('books/', views.BookListView.as_view(), name='books'),
path('book/<int:pk>', views.BookDetailView.as_view(), name='book-detail'),
path('authors/', views.AuthorListView.as_view(), name='authors'),
path('author/<int:pk>', views.AuthorDetailView.as_view(), name='author-detail'),
path('mybooks/', views.LoanedBooksByUserListView.as_view(), name='my-borrowed'),
path('borrowed/', views.LoanedBooksByAllUserListView.as_view(), name='all-borrowed'),
]
在views.py
from django.shortcuts import render
# Create your views here.
from .models import Book, Author, BookInstance, Genre
def index(request):
"""View function for home page of site."""
# Generate counts of some of the main objects
num_books = Book.objects.all().count()
num_instances = BookInstance.objects.all().count()
# Available books (status = 'a')
num_instances_available = BookInstance.objects.filter(status__exact='a').count()
# The 'all()' is implied by default.
num_authors = Author.objects.count()
# Number of visits to this view, as counted in the session variable.
num_visits = request.session.get('num_visits', 0)
request.session['num_visits'] = num_visits + 1
context = {
'num_books': num_books,
'num_instances': num_instances,
'num_instances_available': num_instances_available,
'num_authors': num_authors,
'num_visits': num_visits,
}
# Render the HTML template index.html with the data in the context variable.
return render(request, 'index.html', context=context)
from django.views import generic
class BookListView(generic.ListView):
model = Book
paginate_by = 5
class BookDetailView(generic.DetailView):
model = Book
class AuthorListView(generic.ListView):
model = Author
paginate_by = 5
class AuthorDetailView(generic.DetailView):
model = Author
from django.contrib.auth.mixins import LoginRequiredMixin
class LoanedBooksByUserListView(LoginRequiredMixin,generic.ListView):
"""Generic class-based view listing books on loan to current user."""
model = BookInstance
template_name ='catalog/bookinstance_list_borrowed_user.html'
paginate_by = 10
def get_queryset(self):
return BookInstance.objects.filter(borrower=self.request.user).filter(status__exact='o').order_by('due_back')
from django.contrib.auth.mixins import PermissionRequiredMixin
class LoanedBooksByAllUserListView(PermissionRequiredMixin,generic.ListView):
model = BookInstance
template_name ='catalog/allbookinstancelistborrowed.html'
paginate_by = 10
def get_queryset(self):
return BookInstance.objects.filter(status__exact='o').order_by('due_back')
解决方案
推荐阅读
- docker - 在另一个项目的 GitLab CI 作业期间无法访问 GitLab 注册表
- linux - 在没有 root 和特权用户的情况下安排长时间运行的 laravel 任务的方法
- python - 如何使用 pygame.key.get_pressed() 获取一个输入?
- locale - 在 synfony4 如何在不使用首选语言的情况下强制区域设置“en”
- fortran - 如何编写格式语句以从文件中读取实数并将它们存储在数组中?
- c# - 创建使用 Console.Readline(); 或 args[i] 作为输入的 CLI 程序在功能上/实际上更有效吗?
- html - 使用 bootstrap 4.3.1,为什么我的列不能在列表组中工作?
- r - R 闪亮 - insertUI() 的“立即”参数不适用于图像
- r - 了解 R 中的时间序列图和 ggplot2
- html - Angular 材质 MatDefs 出现运行时错误