0

我的第一个问题是数据没有进入数据库。它没有显示任何错误,但也没有存储。我的第二个问题是我正在使用下拉列表中的查询集预先填充表单字段,但它向我显示 id(主键),我想显示其他字段。我该怎么做?

我的观点

def payment(request):
    #form = jobpostForm_first()
    #country_list = Country.objects.all()
    if request.POST:
        form = jobpostForm_detail(request.POST)

        if form.is_valid():
            if '_Submit' in request.POST:
              form.save()
              return HttpResponseRedirect('/thanks/')
    else:
        form = jobpostForm_detail()
        #form.fields['country'].queryset = Country.objects.all()

    c = {}
    c.update(csrf(request))

    return render_to_response('portal/display.html',{
        'form':form
    },context_instance=RequestContext(request))

我的模型:

class jobpostForm_detail(ModelForm):
    class Meta:

        model = payment_detail
        fields = ('payment_type','country')

    def __init__(self, *args, **kwargs):
        super(jobpostForm_detail, self).__init__(*args, **kwargs)

        self.fields['country'].queryset = Country.objects.all()
        self.fields['payment_type'].queryset = Payment_types.objects.all()

        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.add_input(Submit('submit_addcontent', 'Pay'))

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

我的模板:

<form method="post" action="/portal/next/post/" class="blueForms" id="id-jobpostform">


    {% csrf_token %}

    {% crispy form %}

    </form>
4

1 回答 1

0

你为什么要"_Submit"在你的视图中检查?

我建议您阅读如何在视图中使用表单,尤其是评论:

...
def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
...

在您的情况下,您只需要form.save()form.is_valid()

给你的问题:你能在你的表单类中解释一下这段代码吗?错字,我猜?

widgets = {

        }),

}
于 2013-04-02T17:17:52.310 回答