Python之路【第三十四篇】:django日更

一 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进行绑定,更进一步简化操作。

猜你喜欢

转载自www.cnblogs.com/hackerer/p/12347946.html