首页 > 代码库 > Django中forms的一些用法

Django中forms的一些用法

一、原生的form
In your templates
 1                 <form action="" method="POST" class="" role="form">
 2                    <div class="input-group">
 3                         <span class="input-group-addon" ><img src="{% static ‘img/username2.png‘ %}" alt="" width=‘24px‘></span>
 4                         <input type="text" class="form-control" placeholder="username/email" autofocus="" required="" name=‘username‘>
 5                     </div>               
 6                     <div class="input-group">
 7                         <span class="input-group-addon"><img src="{% static ‘img/password2.png‘ %}" alt=""></span>
 8                         <input type="password" class="form-control" placeholder="your password" required="" name=‘password‘>
 9                     </div>                 
10                     <input type="submit" name=‘login‘ value="Login" class="btn  btn-success btn-block">
11                     {% csrf_token %}
12                 </form>

 

In your views

 1 def login(request):
 2     context={}
 3     if request.method==GET:
 4         return render(request,login.html,context)
 5     else:
 6         //获取Login用户信息的2种方式        
 7         username=request.POST[username]
 8         password=request.POST.get("password")
 9         //数据库中创建用户
10         User.objects.create(
11                     username=username,
12                     password=password,
13                 )
14          return HttpResponse("Success done")        

[需要额外添加验证步骤]

二、使用Django form

In your forms.py,

1 from django import forms
2 
3 class Loginfm(forms.Form):
4     # UI显示名称
5     username=forms.CharField(label=用户)
6     password=forms.CharField(label=密码)

In your views.py

 1 from blogapp.forms import Loginfm
 2 def login(request):
 3     context={}
 4     if request.method==GET:
 5         loginfm=Loginfm()
 6         context["loginfm"]=loginfm
 7         
 8     else:
 9         loginfm=Loginfm(request.POST)
10         if loginfm.is_valid():
11             username=loginfm.cleaned_data[username]
12             password=loginfm.cleaned_data[password]
13             User.objects.create(
14                 username=username,
15                 password=password,
16                 email=ab@qq.com,
17                 )
18             return HttpResponse("Form submit success")
19     
20     return render(request,login.html,context)

In your templates,

1  <form action="" method="POST" class="" role="form">
2           {{ loginfm.as_p }} 
3           {% csrf_token %}
4 </form>

[Django根据字段属性可以对字段合法性进行检查,比较适合大量的补充信息提交,可以省去很多验证环节]

--自带样式较为有限,当然你也可以额外补充样式--

三、ModelForm

In your forms.py

1 from blogapp.models import User
2 class Loginfm(forms.ModelForm):
3     # 绑定User类
4     class Meta():
5         model=User
6         # 除了‘ID‘外全部显示,include(‘ID‘)只包含‘ID‘,相反
7         exclude=("id",email)

In your views

 1 def login(request):
 2     context={}
 3     if request.method==GET:
 4         loginfm=Loginfm()
 5         context["loginfm"]=loginfm
 6     else:
 7         loginfm=Loginfm(request.POST)
 8         if loginfm.is_valid():
 9             loginfm.save()
10             return HttpResponse("Form submit success")
11     return render(request,login.html,context)

codes in your templates are the same as above(way2)

 

Django中forms的一些用法