内置编辑通用视图

formView
formView 定义
formView 实现的细节

model.py

from django.db import models


# Create your models here.
class modelForm(models.Model):
    name = models.CharField(max_length=100)  # 名字
    is_delete = models.BooleanField()

    def __str__(self):
        return self.name

forms.py

from django.forms import forms, ModelForm

from the_14.models import modelForm


class SomethingForm(ModelForm):
    class Meta:
        model = modelForm  # 模型表单关联
        # fields = ['name','email'] # 获取指定字段
        fields = '__all__'  # 获取所有字段
        # exclude = ['email']  # 排除字段

views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic import FormView

from the_14.forms import SomethingForm


# Create your views here.
def hello(request):
    return HttpResponse("hello world")


class SomethingView(FormView):
    template_name = 'the_14/something.html'
    form_class = SomethingForm
    success_url = '/the_14/thanks'  # 成功后跳转的页面


def thanks(request):
    return render(request, 'the_14/thanks.html')

something.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>内置编辑通用视图 formView 的内容</h1>
    <form action="{% url 'the_14:SomethingView' %}" method="post">
        {{ form }}
        <input type="submit" value="提交">
    </form>
</body>
</html>

thanks.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>formView 成功后跳转的内容</h1>
</body>
</html>
编辑类通用视图分析法 

▶ 提供了什么功能
         提交数据到数库能功能
▶ 数据从哪里来
         表单绑定了模型
▶ 提供了哪些模板变量
        get_context_data 方法中添加了 form 这个变量
▶ 渲染的模板页
        template_name
▶ 表单从哪里来
        自定义的模型表单
▶ 模型类从哪里来
        自定义的模型类
▶ 重定向的 url 是什么
        success_url

 CreateView —— 创建

▶ 提供了什么功能
            # 从表单创建数据到数据库中去
▶ 数据从哪里来
            # 是我们自己创建的, 所以没有数据
▶ 提供了哪些模板变量
            # form
▶ 渲染的模板页
            # <模型名的小写>_form.html
            # template_name = "the_16/xxxxx.html"

▶ 表单从哪里来
            # form_class --> forms.py 自定义的模型表单
▶ 模型类从哪里来
            # model绑定的属性
▶ 重定向的 url 是什么
            # success_url

class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):

​​​​​​​    SingleObjectTemplateResponseMixin 找 _form.html

model.py

from django.db import models


# Create your models here.
class SomeThing(models.Model):
    name = models.CharField(max_length=100)  # 名字
    is_delete = models.BooleanField()

View.py

from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic import FormView, CreateView

from the_14.forms import SomethingForm
from the_14.models import SomeThing


# Create your views here.
def hello(request):
    return HttpResponse("hello world")


class SomethingView(FormView):
    template_name = 'the_14/something.html'
    form_class = SomethingForm
    success_url = '/the_14/thanks'  # 成功后跳转的页面


def thanks(request):
    return render(request, 'the_14/thanks.html')


class SomethingCreate(CreateView):
    form_class = SomethingForm
    success_url = '/the_14/thanks'
    model = SomeThing

url.py

from django.urls import path
from .views import hello, SomethingView, thanks, SomethingCreate

app_name = 'the_14'  # 命名空间
urlpatterns = [
    path('Hello/', hello),
    path('SomethingView/', SomethingView.as_view(),name='SomethingView'),
    path('thanks/', thanks, name='thanks'),
    path('SomethingCreateView', SomethingCreate.as_view(), name='SomethingCreate'),
]

something_form.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <H1>这是Create的页面</H1>
    <form action="{% url 'the_14:SomethingCreate' %}" method="post" >
        {{ form.as_p }}
        <input type="submit" value="提交">
    </form>
</body>
</html>
UpdateView —— 修改、更新​​​​​​​

 view.py

from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic import FormView, CreateView, UpdateView

from the_14.forms import SomethingForm
from the_14.models import SomeThing


# Create your views here.
def hello(request):
    return HttpResponse("hello world")


class SomethingView(FormView):
    template_name = 'the_14/something.html'
    form_class = SomethingForm
    success_url = '/the_14/thanks'  # 成功后跳转的页面


def thanks(request):
    return render(request, 'the_14/thanks.html')


# CreateView 创建
class SomethingCreate(CreateView):
    form_class = SomethingForm
    success_url = '/the_14/thanks'
    model = SomeThing


# UpdateView 更新,修改
class SomethingUpdate(UpdateView):
    form_class = SomethingForm
    success_url = '/the_14/thanks'
    # model = SomeThing
    queryset = SomeThing.objects.all()  # 查询

url.py

from django.urls import path
from .views import hello, SomethingView, thanks, SomethingCreate, SomethingUpdate

app_name = 'the_14'  # 命名空间
urlpatterns = [
    path('Hello/', hello),
    path('SomethingView/', SomethingView.as_view(),name='SomethingView'),
    path('thanks/', thanks, name='thanks'),
    path('SomethingCreateView/', SomethingCreate.as_view(), name='SomethingCreate'),
    path('SomethingUpdate/<int:pk>', SomethingUpdate.as_view(), name='SomethingUpdate'),
]

用 postman 访问提交

 UpdateView

 ▶ 提供了什么功能
    # 修改数据的功能
▶ 数据从哪里来
    # queryset  pk参数
▶ 提供了哪些模板变量
    # form
▶ 渲染的模板页
    # <模型名的小写>_form.html
    # template_name = "the_16/xxxxx.html"

▶ 表单从哪里来
    # form_class --> forms.py 自定义的模型表单
▶ 模型类从哪里来
    # model绑定的属性, 还有查询集
▶ 重定向的 url 是什么
    # success_url

DeleteView —— 删除

 View.py

# DeleteView 删除
class SomethingDelete(DeleteView):
    form_class = SomethingForm
    success_url = '/the_14/thanks'
    # queryset = SomeThing.objects.all()
    # template_name = 'the_14/something_form.html'  # 指定页面
# 上面是直接删除数据库的内容
    
    def get_success_url(self):
        """
        is_delete 改成1,不删除,只是不对用户展示
        """

url.py

from django.urls import path
from .views import hello, SomethingView, thanks, SomethingCreate, SomethingUpdate, SomethingDelete

app_name = 'the_14'  # 命名空间
urlpatterns = [
    path('Hello/', hello),
    path('SomethingView/', SomethingView.as_view(),name='SomethingView'),
    path('thanks/', thanks, name='thanks'),
    path('SomethingCreateView/', SomethingCreate.as_view(), name='SomethingCreate'),
    path('SomethingUpdate/<int:pk>', SomethingUpdate.as_view(), name='SomethingUpdate'),
    path('SomethingDelete/<int:pk>', SomethingDelete.as_view(), name='SomethingDelete'),
]

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值