Django --Form

1、不使用Form表单

获取前端数据方式


name = request.POST[‘name‘]

name = request.POST.get[‘name‘]

2、使用Form表单

def comment_post(request):
    try:
        comment_form = 
        if comment_form.is_valid():
            
            comment = Comment.objects.create(username=comment_form.cleaned_data["author"],
                                             email=comment_form.cleaned_data["email"],
                                             url=comment_form.cleaned_data["url"],
                                             content=comment_form.cleaned_data["comment"],
                                             article_id=comment_form.cleaned_data["article"],
                                             user=request.user if request.user.is_authenticated() else None)
            comment.save()
        else:
            return render(request, ‘failure.html‘, {‘reason‘: comment_form.errors})
    except Exception as e:
        logger.error(e)
    return redirect(request.META[‘HTTP_REFERER‘])

3、Form表单验证

方式1、表单字段验证器

方式二:clean_filedname验证字段,针对某个字段进行验证

方式三:表单clean方法,可针对整个表单进行验证。

时间: 2025-01-02 07:55:52

Django --Form的相关文章

mysql “group by ”与"order by"的研究--分类中最新的内容

这两天让一个数据查询难了.主要是对group by 理解的不够深入.才出现这样的情况这种需求,我想很多人都遇到过.下面是我模拟我的内容表我现在需要取出每个分类中最新的内容 select * from test group by category_id order by `date` 结果如下明显.这不是我想要的数据,原因是msyql已经的执行顺序是 引用 写的顺序:select ... from... where.... group by... having... order by..执行顺序:

UML基本架构建模--类的辅助信息

 Organizing Attributes and Operations 组织属性和操作 When drawing a class, you don't have to show every attribute and every operation at once. In fact, in most cases, you can't (there are too many of them to put in one figure) and you probably should not

微信支付--入门篇

公众号支付有2种支付方式: JS API 支付:是指用户打开图文消息或者扫描二维码,在微信内置浏览器打开网页进行的支付.商户网页前端通过使用微信提供的 JS API,调用微信支付模块.这种方式,适合需要在商户网页进行选购下单的购买流程.  Native(原生)支付:是指商户组成符合 Native(原生)支付规则的 URL 链接,用户可通过点击该链接或者扫描对应的二维码直接进入微信支付模块(微信客户端界面),即可进行支付.这种方式,适合无需选购直接支付的购买流程. 以上两种方式最大的区别是:是否需

python第七十七天---HTML

HTML5 :规则, 浏览器的通用规则 1 1.规则, 浏览器的通用规则 2 2.开发者: 3 学习html 规则 4 开发后台程序 5 - 写html文件 (当作模板) 6 - 数据库获取数据,替换到指定的HTML文件中的位置 7 3.本地测试 8 - 找到文件,用浏览器直接打开 9 - pycharm打开测试 10 4.编写 html 文件 11 - doctype 对应关系 12 - html 标签,标签有内部属性 一个页面只能有一个html标签 13 - 注释 <!-- --> 14

UML基本架构建模--关联的术语和概念(二)

 Associations 联合 An association is a structural relationship that specifies that objects of one thing are connected to objects of another. Given an association connecting two classes, you can relate objects of one class to objects of the other clas

UML基本架构建模--开始构建图

 When you work with an architect to design a house, you start with three things: a list of wants (such as "I want a house with three bedrooms" and "I want to pay no more than x"), a few simple sketches or pictures from other houses r

@修饰符--python中的装饰器

http://blog.csdn.net/shangzhihaohao/article/details/6928808 装饰器模式可以在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责,也能够处理那些可以撤销的职责.经常用于日志记录.性能测试等场合. 想象一下这个很常见的场景,你写了一个方法只提供给以登陆的用户访问(事实上我也是通过django的@login_required才了解到@修饰符的),你可以写以下代码: 这当然没什么问题,但是你又写了一个方法B,也要求只有登录用户可以访问

《博客园精华集--Sharepoint分册》第三轮结果

<博客园精华集--Sharepoint分册>第三轮结果 写在前面:   Sharepoint接触的人相对比较少,筛选下来感觉精华文章不多.对于本分册我准备采用入门.使用.开发.进阶的组织方式,当然如果大家认为不够精华有违精华集的宗旨的话,也可以将本分册拿掉,把其中的精华文章合并到其他分册. 下面是我初步筛选的结果,时间.能力有限,难免有疏漏之处,希望大家能够提出宝贵意见,如分类不正确.转载文章.质量太差等,我们将根据大家的意见在后面的工作中进行调整. 另外,如果您的文章当前已经被收录,但您不想

UML实例--HELL_WORLD(六)

 Artifacts 工件 "Hello, World!" is implemented as an applet, so it never stands alone but instead is typically a part of some Web page. The applet starts when its enclosing page is opened, triggered by some browser mechanism that runs the applet

UML基本架构建模--获取类

 Getting Started 开始 Modeling a system involves identifying the things that are important to your particular view. These things form the vocabulary of the system you are modeling. For example, if you are building a house, things like walls, doors, w