在模型窗体按钮django上执行不同的功能 [英] performing different functionality on modelform buttons django

查看:175
本文介绍了在模型窗体按钮django上执行不同的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个按钮的模型,我想对它们执行不同的功能。
我的modelform:

  class jobpostForm(ModelForm):
class Meta:

model = jobpost
fields =('job_title','job_type','job_location','job_description','start_date','end_date','country','how_to_apply')

widgets = {

'job_type':RadioSelect(),
'job_location':TextInput(attrs = {'size':'70'}),
'job_description ':Textarea(attrs = {'cols':200,'rows':10}),
'start_date':TextInput(attrs = {
'class':'datepicker',
'data-date-format':'mm / dd / yyyy',

}),
'end_date':TextInput(attrs = {
'class':'datepicker ',
'data-date-format':'mm / dd / yyyy',
}),

}

def __init __(self ,* args,** kwargs):
#sup er(jobpostForm,self).__ init __(* args,** kwargs)
#self.fields ['start_date']。widget.attrs ['class'] ='datepicker'
super(jobpostForm,自我).__ init __(* args,** kwargs)
#self.fields ['ref_id']。widget = forms.HiddenInput()
self.helper = FormHelper()
self。 helper.form_class ='horizo​​ntal-form'
self.helper.form_id ='id-jobpostform'
self.helper.form_class ='blueForms'
self.helper.form_method ='post'

self.helper.form_action ='/ portal / next / post /'

self.helper.add_input(Submit('submit_addcontent','Preview'))
self.helper.add_input(Submit('submit_addcontent','Submit'))


super(jobpostForm,self).__ init __(* args,** kwargs)

我想在提交和预览中执行不同的功能。我如何在我的视图中访问它们?

解决方案

django表单真的处理两件事:




  • 在GET请求上显示初始表单

  • 使用数据处理POST请求



您可以通过多种方式来处理您的情况。一种方式是让两个按钮都提交你的表单。预览按钮将填写名为预览的隐藏字段。您的表单将处理提交的数据。如果数据包含名为预览的POST字段中的值,则会呈现预览。否则,它将正常处理形式。


I have a modelform with 2 buttons and i want to perform different functionality on them. My modelform:

class jobpostForm(ModelForm):
    class Meta:

        model = jobpost
        fields = ('job_title','job_type','job_location','job_description','start_date','end_date','country','how_to_apply')

    widgets = {

        'job_type':RadioSelect(),    
        'job_location':TextInput(attrs={'size':'70'}),   
        'job_description':Textarea(attrs={'cols':200, 'rows':10}),
            'start_date':TextInput(attrs={
                'class': 'datepicker',
                'data-date-format': 'mm/dd/yyyy', 

            }),
            'end_date':TextInput(attrs={
                'class': 'datepicker',
                'data-date-format': 'mm/dd/yyyy', 
            }),

    }

    def __init__(self, *args, **kwargs):
        #super(jobpostForm, self).__init__(*args, **kwargs)
        #self.fields['start_date'].widget.attrs['class'] = 'datepicker'
        super(jobpostForm, self).__init__(*args, **kwargs)
        #self.fields['ref_id'].widget = forms.HiddenInput()
        self.helper = FormHelper()
        self.helper.form_class = 'horizontal-form'
        self.helper.form_id = 'id-jobpostform'
        self.helper.form_class = 'blueForms'
        self.helper.form_method = 'post'

        self.helper.form_action = '/portal/next/post/'

        self.helper.add_input(Submit('submit_addcontent', 'Preview'))
        self.helper.add_input(Submit('submit_addcontent', 'Submit'))


        super(jobpostForm, self).__init__(*args, **kwargs)

I want to perform different functionality on submit and preview.How can i access them in my view?

解决方案

A django form really handles two things:

  • Displaying the intitial form on a GET request
  • Processing POST requests with data

You can approach your situation in multiple ways. One way would be to have both buttons submit your form. The preview button would fill in a hidden field named preview. Your form would process the submitted data. If the data included a value in the POST field named preview it would render a preview. Otherwise, it would process the form normally.

这篇关于在模型窗体按钮django上执行不同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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