栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

django.views.generic通用视图的CreateView, ListView, UpdateView, DetailView, DeleteView用法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

django.views.generic通用视图的CreateView, ListView, UpdateView, DetailView, DeleteView用法

导入:

from django.views.generic import CreateView, ListView, UpdateView, DetailView, DeleteView

        用过django的人对视图函数并不陌生,generic 的通用视图用法很简单,所以在讲使用方法之前先讲几个个入坑指南吧。

坑1:这个坑主要在于参数“context_object_name",话不多说,直接入题:

        在使用ListView和DeleteView的时候,如果你没有更改类视图中默认的context_object_name,一般不会出问题。但是如果更改了,请注意在不同的函数对应的模板中的参数使用。

在视图函数listView和list.html中传递的参数。

在视图函数deleteView和delete.html中传递的参数。

 坑2:No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.

原因:如果没有success_url

# 增加博客目录
class CheditorCategoryCreateView(LoginRequiredMixin, CreateView):
    model = ArticleCategory
    template_name = 'cheditorBlog/category_create.html'
    fields = ['title']

    # 如果没有success_url,会报错No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.
    success_url = reverse_lazy('blog:cheditorCategory_list')

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/725009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号