在中使用类时,窗体操作在Django中的工作方式视图.py用于窗体创建

2024-06-25 23:44:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在根据我的模型在django中构建一个表单。我把这个放在我的视图.py公司名称:

class GroupCreateView(CreateView):
    model = Group
    form_class = GroupForm
    template_name = 'ipaswdb/group/group_form.html'

这个模板看起来很不错,有一些漂亮的css和一个日期选择器之类的。我还创建了一个表单,以便在我的表单.py在

^{pr2}$

所有这些对我来说都是有意义的,我可以看到表单是如何基于我的模型构建的,并且填充了ModelChoiceField等内容。。我只是不知道我的视图.py发挥作用。因此,在表单操作的表单模板中,我有以下内容:

<h1> Add a new Group </h1>
    <form action="." method="post">
    {% csrf_token %}
  <div class="col-2">
    {{ form.group_name.errors }}
    <label>
      Group Name:
      <input placeholder="Enter the groups name" id="id_group_name" name="group_name" tabindex="1">
    </label>
  </div>
  <div class="col-2">
        {{ form.group_contact.errors }}

    <label>
      Gorup Contact
      <input placeholder="Enter the groups contact name" id="id_group_contact" name="group_contact" tabindex="2">
    </label>
  </div>

  <div class="col-2">
    {{ form.tin.errors }}
    <label>
      TIN Number
      <input placeholder="Groups TIN#" id="id_tin" name="tin" tabindex="3">
    </label>
  </div>
  <div class="col-2">
    {{ form.npi.errors }}
    <label>
      NPI Number
      <input placeholder="Groups NPI#" id="id_npi" name="npi" tabindex="4">
    </label>
    etc etc etc

我认为它调用了视图中的某个默认方法?我只是不知道那是什么方法。这也是为了添加一个新的组,我猜我需要另一个视图或其他东西来处理他们正在编辑一个已经存在的组的情况?我使用的这个博客演示在视图.py用一个表单.py正如我们所想的那样,这里没有GroupCreateView(CreateView):我在视图.py,有这个方法(注意不是我的方法):

def post_detail(request, year, month, day, post):
    post = get_object_or_404(Post, slug=post,
                            status='published',
                            publish__year=year,
                            publish__month=month,
                            publish__day=day)

    comments = post.comments.filter(active=True)
    #their form stuff
    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.save()
    else:
            comment_form=CommentForm()

    return render(request,
                    'blog/post/detail.html',
                    {'post': post, 'comments':comments, 'comment_form':comment_form})

我的问题是(我不记得引用什么例子了),但是class GroupCreateView(CreateView):到底在做什么,我如何让它引用/创建的表单返回来调用正确的操作,最终让我验证并保存到数据库中?在

第二个问题是,我如何扩展(粗略地)来处理它正在添加一个新组的情况,也可能是另一个正在编辑一个现有组的情况?(我在这里提出第二个问题,因为我确信它与第一个问题的答案有关)。在

从我的网址.py在

    url(r'group/add/$', GroupCreateView.as_view(), name='group-add'), 

Tags: namepydivform视图id表单new
1条回答
网友
1楼 · 发布于 2024-06-25 23:44:32

不要害怕阅读django的源代码:p,泛型类有两个方法:“get”和“post”(和“put”也一样,但它称为“post”),如果需要,可以覆盖其中任何一个。在

class BaseCreateView(ModelFormMixin, ProcessFormView):
    """
    Base view for creating an new object instance.

    Using this base class requires subclassing to provide a response mixin.
    """
    def get(self, request, *args, **kwargs):
        self.object = None
        return super(BaseCreateView, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.object = None
        return super(BaseCreateView, self).post(request, *args, **kwargs)

但它也继承了它父母的方法,所以它可能有点难以阅读。我总是检查docs for the generic views,它提供了一个列表,列出了可以在每个泛型类上覆盖的所有方法。现在,您可以覆盖所需的所有方法,而无需重复代码(这就是为什么I<;3cbv)

我认为在您的例子中,在重定向到成功页面之前,您可能需要重写^{}方法来执行某些操作

希望这有帮助

相关问题 更多 >