视图类
显示静态模板
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('users_app.urls'))
]
- 在应用的urls.py文件中 配置相关的模板配置 并在模板文件夹当中创建index.html文件
from django.views.generic import TemplateView
urlpatterns = [
url(r'^index/$', TemplateView.as_view(template_name="index.html"))
]
继承视图类
# 导入
from django.views.generic import View, ListView, TemplateView
# 基本使用
class DemoView(View):
def get(self, request):
pass
def post(self, request):
pass
继承listView试图类
from users_app.models import BookInfo
class ShowBookInfo(ListView):
# model = BookInfo # 指定模型对象 默认将查询结果传递到模板当中 变量名字为 模型类名_list/object_list
queryset = BookInfo.objects.all().order_by('-pk') # 指定一个查询集将上下文对象
context_object_name = 'wangxiaofei' # 指定上下文传递到对象的名字
template_name = 'users_app/bookinfo_list.html'
def get_context_data(self, **kwargs):
"""调用此方法将数据传递到html页面当中"""
print 2
context = super(ShowBookInfo, self).get_context_data(**kwargs)
return context
def get_queryset(self):
"""指定查询集对象在类属性queryset后执行"""
print 1
self.res = self.args[0] # url参数中的参数从self.args中获取 命名参数从self.kwargs中获取
return BookInfo.objects.all()