📌  相关文章
📜  FormView – 基于类的视图 Django

📅  最后修改于: 2022-05-13 01:55:39.440000             🧑  作者: Mango

FormView – 基于类的视图 Django

FormView 是指用于显示和验证 Django 表单的视图(逻辑)。例如在 geeksforgeeks 注册用户的表格。基于类的视图提供了另一种将视图实现为Python对象而不是函数的方法。它们不会取代基于函数的视图,但与基于函数的视图相比,它们具有一定的区别和优势:

  • 与特定 HTTP 方法(GET、POST 等)相关的代码组织可以通过单独的方法而不是条件分支来解决。
  • 诸如混合(多重继承)之类的面向对象技术可用于将代码分解为可重用的组件。

基于类的视图比基于函数的视图更简单、更有效地管理。具有大量代码行的基于函数的视图可以转换为仅具有几行代码的基于类的视图。这就是面向对象编程产生影响的地方。

Django FormView – 基于类的视图

如何使用示例创建和使用 FormView的说明。考虑一个名为 geeksforgeeks 的项目,它有一个名为 geeks 的应用程序。

在你有一个项目和一个应用程序之后,让我们创建一个我们将创建 FormView 的表单。在 geeks/forms.py 中,

Python3
from django import forms
 
# creating a form
class GeeksForm(forms.Form):
    # specify fields for model
    title = forms.CharField()
    description = forms.CharField(widget = forms.Textarea)


Python3
# import generic FormView
from django.views.generic.edit import FormView
 
# Relative import of GeeksForm
from .forms import GeeksForm
 
class GeeksFormView(FormView):
    # specify the Form you want to use
    form_class = GeeksForm
     
    # specify name of template
    template_name = "geeks / geeksmodel_form.html"
 
    # can specify success url
    # url to redirect after successfully
    # updating details
    success_url ="/thanks/"


html
    {% csrf_token %}     {{ form.as_p }}     


Python3
from django.urls import path
 
# importing views from views..py
from .views import GeeksFormView
urlpatterns = [
    path('', GeeksFormView.as_view()),
]


Python3
# import generic FormView
from django.views.generic.edit import FormView
 
# Relative import of GeeksForm
from .forms import GeeksForm
 
class GeeksFormView(FormView):
    # specify the Form you want to use
    form_class = GeeksForm
     
    # specify name of template
    template_name = "geeks / geeksmodel_form.html"
 
    # can specify success url
    # url to redirect after successfully
    # updating details
    success_url ="/thanks/"
 
    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
         
        # perform a action here
        print(form.cleaned_data)
        return super().form_valid(form)


创建表单后,让我们创建FormView 。在极客/views.py 中,

Python3

# import generic FormView
from django.views.generic.edit import FormView
 
# Relative import of GeeksForm
from .forms import GeeksForm
 
class GeeksFormView(FormView):
    # specify the Form you want to use
    form_class = GeeksForm
     
    # specify name of template
    template_name = "geeks / geeksmodel_form.html"
 
    # can specify success url
    # url to redirect after successfully
    # updating details
    success_url ="/thanks/"

在 geeks/geeksmodel_form.html 中为此视图创建模板,

html

    {% csrf_token %}     {{ form.as_p }}     

在 geeks/urls.py 中将一个 url 映射到这个视图,

Python3

from django.urls import path
 
# importing views from views..py
from .views import GeeksFormView
urlpatterns = [
    path('', GeeksFormView.as_view()),
]

现在访问http://127.0.0.1:8000/,

django-create-view-function-based

在表单视图中验证表单数据

基于类的视图提供了用于验证表单数据的内置函数。在 geeks/views.py 中,

Python3

# import generic FormView
from django.views.generic.edit import FormView
 
# Relative import of GeeksForm
from .forms import GeeksForm
 
class GeeksFormView(FormView):
    # specify the Form you want to use
    form_class = GeeksForm
     
    # specify name of template
    template_name = "geeks / geeksmodel_form.html"
 
    # can specify success url
    # url to redirect after successfully
    # updating details
    success_url ="/thanks/"
 
    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
         
        # perform a action here
        print(form.cleaned_data)
        return super().form_valid(form)

可以在 form_valid 函数中执行所需的功能。在我们的例子中,它打印数据。让我们尝试在表单中输入数据并检查它是否有效。

django-formview-class-based-view

检查终端是否已打印详细信息。

django-formview-终端