码迷,mamicode.com
首页 > 其他好文 > 详细

Form和ModelForm

时间:2017-10-10 01:19:33      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:标签   go import   code   field   ges   data   rbac   html   mod   

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

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

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

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

form实例

# app01/views.py
from django.shortcuts import render, HttpResponse
                                                  
# Create your views here.
# from django import forms
from django import forms
                                                  
                                                  
class LoginForm(forms.Form):
    user=forms.CharField(min_length=5,
                         max_length=12,
                         error_messages={required:"该内容不能为空"},                                widget=forms.TextInput(attrs={"class":"s",placeholder:user})
                         )
    pwd=forms.CharField()
                                                         
                                                           
def login(request):
    if request.method=="POST":
        form_obj=LoginForm(request.POST)
        if form_obj.is_valid():
            print(form_obj.cleaned_data)
        else:
            print(form_obj.errors)
        return HttpResponse("OK")
    form_obj=LoginForm()
    return render(request, "login.html",{form_obj: form_obj})
 
# template/login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/login/" method="POST">
    {% csrf_token %}
    {{ form_obj.as_p }}
    <button>提交</button>
</form>
</body>
</html>

###或
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/login/" method="POST">
    {% csrf_token %}
    {{ form_obj.user }}
    {{ form_obj.pwd }}
    <button>提交</button>
</form>
</body>
</html>
##注意事项
1) {% csrf_token %}
2)  html中button标签在form表单中具有提交功能,在form表单外不具有提交功能
{{ form_obj.as_p }}
{{ form_obj.as_ul }}
{{ form_obj.as_table }}

ModelForm实例

from django.shortcuts import render, redirect, HttpResponse
from . import models
# Create your views here.
from django.forms import ModelForm
from django.forms import widgets as wid
from django.forms import fields as fld
                                        
                                        
class UserModelForm(ModelForm):
    class Meta:                 
        model = models.UserInfo
        fields = "__all__"              
        fields = [username,nickname]
        exclude = [username]                       
        error_messages={                             
            username: {required: 用户名不能为空},
        }                                            
        widgets= {                                   
            username: wid.Textarea(attrs={class:"c1"})
        }                                            
        labels={                                     
            username: 用户名,                    
        }                                            
        help_texts= {                                
            username: 别瞎写,.......             
        }                                            
        field_classes= {                            
            nickname: fld.EmailField              
        }                                           
                                 
                                 
class RoleModelForm(ModelForm):
    class Meta:                 
        model = models.Role      
        fields = "__all__"       
                                       
                                       
class PermissionModelForm(ModelForm):
    class Meta:                 
        model = models.Permission
        fields = "__all__"       
                                 
                                 
class MenuModelForm(ModelForm):
    class Meta:                 
        model = models.Menu      
        fields = "__all__"       
                                            
                                            
def users(request):                        
    user_list= models.UserInfo.objects.all()
    return render(request,"rbac/users.html",{user_list:user_list})
                               
                               
def user_add(request):
    if request.method == "GET":
        model_form=UserModelForm()
        return render(request, "rbac/user_add.html", {model_form:model_form})                    
    else:                                     
        model_form=UserModelForm(request.POST)
        if model_form.is_valid():
            model_form.save()
            return redirect("/rbac/users/")
        return render(request, "rbac/user_add.html", {model_form: model_form})                                          
                                                      
                                                      
def user_del(request,pk):                            
    obj_queryset=models.UserInfo.objects.filter(pk=pk)
    if obj_queryset:                                  
        obj_queryset.delete()                         
    return redirect("/rbac/users/")                    
                                                       
                                                       
def user_edit(request,pk):
    obj = models.UserInfo.objects.filter(pk=pk).first()
    if not obj:                        
        return redirect("/rbac/users/")
    if request.method=="GET":
        model_form=UserModelForm(instance=obj)
        return                        render(request,rbac/user_edit.html,{model_form:model_form})
    else:                                                 
        model_form=UserModelForm(request.POST,instance=obj)
        if model_form.is_valid():                        
            model_form.save()                             
            return redirect("/rbac/users/")                
        return render(request, rbac/user_edit.html, {model_form: model_form})
 
 

 

 

Form和ModelForm

标签:标签   go import   code   field   ges   data   rbac   html   mod   

原文地址:http://www.cnblogs.com/NewTaul/p/7643547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!