入门笔记翻译整理自:https://docs.djangoproject.com/en/1.8/topics/
*该笔记将对各个模块进行单独介绍
*Forms
1. 使用表单(Working with forms)
只要网站涉及到访问者的输入操作,那么就必须用到表单。在HTML中,表单是<form>...</form>中的语句集合。
GET和POST是HTTP处理表单仅有的两种方式。Django中使用Form类表示表单。
对使用方法进行简单举例:
1 # forms.py 2 3 from django import forms 4 5 class NameForm(forms.Form): 6 your_name = forms.CharField(label=‘Your name‘, max_length=100)
1 # view.py 2 3 from django.shortcuts import render 4 from django.http import HttpResponseRedirect 5 6 from .forms import NameForm 7 8 def get_name(request): 9 # if this is a POST request we need to process the form data 10 if request.method == ‘POST‘: 11 # create a form instance and populate it with data from the request: 12 form = NameForm(request.POST) 13 # check whether it‘s valid: 14 if form.is_valid(): 15 # process the data in form.cleaned_data as required 16 # ... 17 # redirect to a new URL: 18 return HttpResponseRedirect(‘/thanks/‘) 19 20 # if a GET (or any other method) we‘ll create a blank form 21 else: 22 form = NameForm() 23 24 return render(request, ‘name.html‘, {‘form‘: form})
1 # name.html 2 3 <form action="/your-name/" method="post"> 4 {% csrf_token %} 5 {{ form }} 6 <input type="submit" value="Submit" /> 7 </form>
2. 表单集合(Formsets)
Formsets可以一次性创建多个表格。使用举例如下:
1 >>> import datetime 2 >>> from django.forms.formsets import formset_factory 3 >>> from myapp.forms import ArticleForm 4 >>> ArticleFormSet = formset_factory(ArticleForm, extra=2) 5 >>> formset = ArticleFormSet(initial=[ 6 ... {‘title‘: ‘Django is now open source‘, 7 ... ‘pub_date‘: datetime.date.today(),} 8 ... ]) 9 10 >>> for form in formset: 11 ... print(form.as_table()) 12 <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Django is now open source" id="id_form-0-title" /></td></tr> 13 <tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" value="2008-05-12" id="id_form-0-pub_date" /></td></tr> 14 <tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" id="id_form-1-title" /></td></tr> 15 <tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" id="id_form-1-pub_date" /></td></tr> 16 <tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr> 17 <tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr>
在formset_factory中还可以添加max_num,validate_max,can_order,can_delete属性,Formset还具有is_valid()方法。
3. 从模型创建表单
举例如下:
1 >>> from django.forms import ModelForm 2 >>> from myapp.models import Article 3 4 # Create the form class. 5 >>> class ArticleForm(ModelForm): 6 ... class Meta: 7 ... model = Article 8 ... fields = [‘pub_date‘, ‘headline‘, ‘content‘, ‘reporter‘] 9 10 # Creating a form to add an article. 11 >>> form = ArticleForm() 12 13 # Creating a form to change an existing article. 14 >>> article = Article.objects.get(pk=1) 15 >>> form = ArticleForm(instance=article)
4. 表单资产(Form Assets)
核心内容:css,js,media, widget,略过。
-- The End --
时间: 2024-10-01 00:37:37