我的第一个问题是数据没有进入database.it没有显示任何错误,但也没有存储。 我的第二个问题是我预先填写表格字段,在下拉列表中设置查询,但它显示我的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>
答案 0 :(得分:0)
为什么要在视图中查看"_Submit"
?
我建议你阅读how to use a form in a view,特别是评论:
...
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 = {
}),
}