How implement Django.views.generic if earlier used request?
from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models import *
from django.contrib.auth import login,logout,authenticate
from .forms import *
from django.views.generic import ListView
# Create your views here.
# New
class HomePage(ListView):
model = Book
template_name = 'book/templates/home.html'
# Old
def home(request):
books=Book.objects.all()
context={'books':books}
if request.user.is_staff:
return render(request,'book/templates/adminhome.html',context)
else:
return render(request,'book/templates/home.html',context)
CodePudding user response:
You can make ListView
in following way:
class BookListView(ListView):
model=Book
context_object_name='books'
def get_template_names(self):
if self.request.user.is_staff:
return ['book/templates/adminhome.html']
else:
return ['book/templates/home.html']
Note:
Class based views requires their names to be written as model name as the prefix and actual view name as the suffix, so I changed it to BookListView
from .HomePage