zoukankan      html  css  js  c++  java
  • Python之路【第三十四篇】:django admin配置

    一 admin的配置

    django admin是django自带的一个后台app,提供了后台的管理功能。

    基础知识点:

    一  认识ModelAdmin

       管理界面的定制类,如需扩展特定的model界面需从该类继承

    二 注册medel类到admin的两种方式:

         1   使用register的方法

         2   使用register的装饰器

    三 掌握一些常用的设置技巧

        list_display:  指定要显示的字段

        search_fields:指定搜索的字段

        list_filter: 指定列表过滤器

        ordering:指定排序字段

        

    三 Form

    一 什么是Form?什么是DjangoForm?

    Django表单系统中,所有的表单类都作为django.forms.Form的子类创建,包括ModelForm

    关于django的表单系统,主要分两种

    基于django.forms.Form:所有表单类的父类

    基于django.forms.ModelForm:可以和模型类绑定的Form

    实例:实现添加出版社信息的功能

    二 不使用Django Form的情况

        

    三  使用Form的情况

        首先,在app01中建立forms.py

    ######################################################### 
    
    
    #app01下新建的forms.py
    from django import forms
    
    class Mypub_form(forms.Form):
        name = forms.CharField(label='名称',error_messages={'required':'必填'})
        address = forms.CharField(label='地址',error_messages={'required':'必填'})
        city = forms.CharField(label='城市',error_messages={'required':'必填'})
        state_province = forms.CharField(label='省份',error_messages={'required':'必填'})
        country = forms.CharField(label='国家',error_messages={'required':'必填'})
        website = forms.URLField(label='网址',error_messages={'required':'必填'})
    #######################################################
    #app01.views
    def add_publisher(req):
        if req.method=='POST':
            # #不使用django form
            # print(req.POST)
            # name=req.POST['name']
            # address=req.POST.get('address')
            # city=req.POST['city']
            # province=req.POST['province']
            # country=req.POST['country']
            # website=req.POST['website']
            # Publisher.objects.create(
            #     name=name,
            #     city=city,
            #     address=address,
            #     state_province=province,
            #     country=country,
            #     website=website
            # )
            # return HttpResponse("添加出版社信息成功!")
    
            #使用django form的情况
    
            Mypub_form_obj=Mypub_form(req.POST)
            if Mypub_form_obj.is_valid():
                Publisher.objects.create(
                    name=Mypub_form_obj.cleaned_data["name"],
                    address=Mypub_form_obj.cleaned_data["address"],
                    city=Mypub_form_obj.cleaned_data["city"],
                    state_province=Mypub_form_obj.cleaned_data["state_province"],
                    country=Mypub_form_obj.cleaned_data["country"],
                    website=Mypub_form_obj.cleaned_data["website"],
                )
    
                return HttpResponse("添加出版社信息成功!")
        else:
            Mypub_form_obj=Mypub_form()
        return render(req,'add_publisher.html',locals())
    #######################################################
    #add_publisher.html
    <body>
         <form action="{% url 'add_pub' %}" method="post">
             {% csrf_token %}
    {#           名称:<input type="text" name="name"><br>#}
    {#           地址:<input type="text" name="address"><br>#}
    {#           城市:<input type="text" name="city"><br>#}
    {#           省份:<input type="text" name="province"><br>#}
    {#           国家:<input type="text" name="country"><br>#}
    {#           网址:<input type="text" name="website"><br>#}
    {#           <input type="submit" value="提交"><br>#}
    
    
             {{ Mypub_form_obj.as_p }}
             <input type="submit" value="提交"><br>
         </form>
    </body>

    四 使用ModelForm的情况

    #######################################################
    #app01.views
    def add_publisher(req):
        if req.method=='POST':
            # #不使用django form
            # print(req.POST)
            # name=req.POST['name']
            # address=req.POST.get('address')
            # city=req.POST['city']
            # province=req.POST['province']
            # country=req.POST['country']
            # website=req.POST['website']
            # Publisher.objects.create(
            #     name=name,
            #     city=city,
            #     address=address,
            #     state_province=province,
            #     country=country,
            #     website=website
            # )
            # return HttpResponse("添加出版社信息成功!")
    
            #使用django form的情况
    
            Mypub_form_obj=Mypub_form(req.POST)
            if Mypub_form_obj.is_valid():
                # Publisher.objects.create(
                #     name=Mypub_form_obj.cleaned_data["name"],
                #     address=Mypub_form_obj.cleaned_data["address"],
                #     city=Mypub_form_obj.cleaned_data["city"],
                #     state_province=Mypub_form_obj.cleaned_data["state_province"],
                #     country=Mypub_form_obj.cleaned_data["country"],
                #     website=Mypub_form_obj.cleaned_data["website"],
                # )
                Mypub_form_obj.save()
    
                return HttpResponse("添加出版社信息成功!")
        else:
            Mypub_form_obj=Mypub_form()
        return render(req,'add_publisher.html',locals())
    #######################################################
    #add_publisher.html
    <body>
         <form action="{% url 'add_pub' %}" method="post">
             {% csrf_token %}
    {#           名称:<input type="text" name="name"><br>#}
    {#           地址:<input type="text" name="address"><br>#}
    {#           城市:<input type="text" name="city"><br>#}
    {#           省份:<input type="text" name="province"><br>#}
    {#           国家:<input type="text" name="country"><br>#}
    {#           网址:<input type="text" name="website"><br>#}
    {#           <input type="submit" value="提交"><br>#}
    
    
             {{ Mypub_form_obj.as_p }}
             <input type="submit" value="提交"><br>
         </form>
    </body>

    思考:为什么有的显示汉子,有的显示英文

    总结:

        使用Django中Form可以大大简化代码,常用的表单功能特性都整合到了Form中,而ModelForm可以和Model进行绑定,更进一步简化操作。

  • 相关阅读:
    Dom 动态添加元素节点总结
    SQLserver 获取当前时间
    Var的用法解析
    JS 转换HTML转义符
    20210602---为了养老,全力以赴,珍惜每一秒钟。决心不够大,不够担心未来,现在虽然挣得少,但是有吃有喝,满足了。
    20210601——今天开始狠狠奖励自己,而且是必须玩的这种。做事投入你就会快乐。
    20210531兴趣
    20210527学习笔记--没成功的唯一原因是,想得和说的太多 做的太少。
    20210526--今年还有半年,抓紧一切时间学习
    20210524学习笔记---从记日记开始已经有3个月了,浪费时间的痕迹渐渐清醒
  • 原文地址:https://www.cnblogs.com/hackerer/p/12347946.html
Copyright © 2011-2022 走看看