第七章 表单
2018-11-09 本文已影响0人
Farewell_lpf
def hello(request):
return HttpResponse("hello world")
1.request.path 除域名以外的请求路径 "/hello/"
2.request.get_host() 主机名(域名)
3.request.get_full_path() 请求路径(包含查询字段)
"/hello/?print=true"
request.META
requst.META.get('remote_addr ','unknown') 客户端IP
from django import form
class ContactForm(forms.Form):
subject = forms.CharField()
email = forms.EmailField(required=False)
message = forms.CharField(widget=forms.Textarea)
输出格式:
f=ContactForm()
f.as_table() 默认
f.as_ul()
f.as_p()
数据校验:
f['message'].errors
{{ form.subject.errors }}
f.cleaned_data 包含提交的数据字典
字段设置
widget=forms.Textarea
required=False
max_length=100
min_length=100
自定义验证字段
class ContactForm(forms.Form):
message = forms.CharField(widget=forms.Textarea)
def clean_message(self):
message = self.cleaned_data['message']
num_words = len(message.split())
if num_words < 4:
raise forms.ValidationError("Not enough words!")
return message
forms.ValidationError型异常。这个异常的描述会被作为错误列表中的一项显示给用户