当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Django FormView用法及代码示例


本文介绍 django.views.generic.edit.FormView 的用法。

声明

class django.views.generic.edit.FormView

显示表单的视图。出错时,重新显示带有验证错误的表单;成功后,重定向到一个新的 URL。

祖先 (MRO)

此视图从以下视图继承方法和属性:

示例 myapp/forms.py

from django import forms

class ContactForm(forms.Form):
    name = forms.CharField()
    message = forms.CharField(widget=forms.Textarea)

    def send_email(self):
        # send email using the self.cleaned_data dictionary
        pass

示例 myapp/views.py

from myapp.forms import ContactForm
from django.views.generic.edit import FormView

class ContactFormView(FormView):
    template_name = 'contact.html'
    form_class = ContactForm
    success_url = '/thanks/'

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        form.send_email()
        return super().form_valid(form)

示例 myapp/contact.html

<form method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Send message">
</form>

相关用法


注:本文由纯净天空筛选整理自djangoproject.com大神的英文原创作品 django.views.generic.edit.FormView。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。