Django:基于类的视图可以一次接受两种形式吗? [英] Django: Can class-based views accept two forms at a time?

查看:89
本文介绍了Django:基于类的视图可以一次接受两种形式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两种形式:

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

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

,并希望使用基于类的视图,发送两个表单到模板,这是否可能?

  class TestView(FormView):
template_name ='contact .html'
form_class = ContactForm

似乎FormView只能接受一个表单时间。
在基于功能的视图中,虽然我可以轻松地将两个表单发送到我的模板并检索请求中的内容.POST。

 code> variables = {'contact_form':contact_form,'social_form':social_form} 
return render(request,'discussion.html',variables)
pre>

这是使用基于类的视图(通用视图)的限制吗?



非常感谢

解决方案

这是一个可扩展的解决方案。我的起点是这个要点,



https://gist.github。 com / michelts / 1029336



我已经增强了该解决方案,以便可以显示多个表单,但可以提交全部或个人。



https://gist.github.com/jamesbrobb/748c47f46b9bd224b07f



这是一个示例用法

  class SignupLoginView(MultiFormsView): 
template_name ='public / my_login_signup_template.html'
form_classes = {'login':LoginForm,
'signup':SignupForm}
success_url ='my / success / url'

def get_login_initial(self):
return {'email':'dave@dave.com'}

def get_signup_initial(self):
return {'email':'dave@dave.com'}

def get_context_data(self,** kwarg s)
context = super(SignupLoginView,self).get_context_data(** kwargs)
context.update({some_context_value:'blah blah blah',
some_other_context_value blah'})
返回上下文

def login_form_valid(self,form):
return form.login(self.request,redirect_url = self.get_success_url())

def signup_form_valid(self,form):
user = form.save(self.request)
return form.signup(self.request,user,self.get_success_url())

,模板看起来像这样

 < form class =loginmethod =POSTaction ={%url'my_view'%}> 
{%csrf_token%}
{{forms.login.as_p}}

< button name ='action'value ='login'type =submit>登录< / button>
< / form>

< form class =signupmethod =POSTaction ={%url'my_view'%}>
{%csrf_token%}
{{forms.signup.as_p}}

< button name ='action'value ='signup'type =submit>注册< / button>
< / form>

模板上要注意的一个重要事情是提交按钮。他们必须将他们的'name'属性设置为'action',并且它们的'value'属性必须与'form_classes'dict中赋予表单的名称相匹配。这用于确定提交的个人表单。


If I have two forms:

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

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

and wanted to use a class based view, and send both forms to the template, is that even possible?

class TestView(FormView):
    template_name = 'contact.html'
    form_class = ContactForm

It seems the FormView can only accept one form at a time. In function based view though I can easily send two forms to my template and retrieve the content of both within the request.POST back.

variables = {'contact_form':contact_form, 'social_form':social_form }
return render(request, 'discussion.html', variables)

Is this a limitation of using class based view (generic views)?

Many Thanks

解决方案

Here's a scaleable solution. My starting point was this gist,

https://gist.github.com/michelts/1029336

i've enhanced that solution so that multiple forms can be displayed, but either all or an individual can be submitted

https://gist.github.com/jamesbrobb/748c47f46b9bd224b07f

and this is an example usage

class SignupLoginView(MultiFormsView):
    template_name = 'public/my_login_signup_template.html'
    form_classes = {'login': LoginForm,
                    'signup': SignupForm}
    success_url = 'my/success/url'

    def get_login_initial(self):
        return {'email':'dave@dave.com'}

    def get_signup_initial(self):
        return {'email':'dave@dave.com'}

    def get_context_data(self, **kwargs):
        context = super(SignupLoginView, self).get_context_data(**kwargs)
        context.update({"some_context_value": 'blah blah blah',
                        "some_other_context_value": 'blah'})
        return context

    def login_form_valid(self, form):
        return form.login(self.request, redirect_url=self.get_success_url())

    def signup_form_valid(self, form):
        user = form.save(self.request)
        return form.signup(self.request, user, self.get_success_url())

and the template looks like this

<form class="login" method="POST" action="{% url 'my_view' %}">
    {% csrf_token %}
    {{ forms.login.as_p }}

    <button name='action' value='login' type="submit">Sign in</button>
</form>

<form class="signup" method="POST" action="{% url 'my_view' %}">
    {% csrf_token %}
    {{ forms.signup.as_p }}

    <button name='action' value='signup' type="submit">Sign up</button>
</form>

An important thing to note on the template are the submit buttons. They have to have their 'name' attribute set to 'action' and their 'value' attribute must match the name given to the form in the 'form_classes' dict. This is used to determine which individual form has been submitted.

这篇关于Django:基于类的视图可以一次接受两种形式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆