我想用數據庫數據來模擬我的modelform字段並在單選按鈕中顯示它們。使用數據庫填充modelform字段-django
這是我的ModelForm:
class jobpostForm_detail(ModelForm):
class Meta:
model = payment_detail
fields = ('payment_type','country')
widgets = {
'payment_type':RadioSelect(),
'country':RadioSelect(),
}
def __init__(self, *args, **kwargs):
super(jobpostForm_detail, self).__init__(*args, **kwargs)
self.fields['country'].queryset = Country.objects.all() // This is not showing data in radio buttons.
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 = '/'
self.helper.add_input(Submit('submit_addcontent', 'Pay'))
super(jobpostForm_detail, self).__init__(*args, **kwargs)
國模型:
class Country(models.Model):
country_id = models.AutoField(primary_key=True)
country_name = models.CharField(max_length=255,null=True, unique=True)
def __unicode__(self):
return unicode(self.country_id)
return unicode(self.country_name)
模板:
<form method="post" action="/portal/next/post/" class="blueForms" id="id-jobpostform">
{% csrf_token %}
{% crispy form %}
</form>
This is my view:
def payment(request):
#form = jobpostForm_first()
country_list = Country.objects.all()
if request.method == 'POST':
form = jobpostForm_detail(request.POST)
#if form.is_valid():
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,'country_list':country_list
},context_instance=RequestContext(request))
我的數據也不會在數據庫中去
我想顯示國家y names ..它顯示我的國家ID爲
我上面做了什麼顯示我的國家的身份證。我想顯示名字..你可以告訴我我該怎麼做? –
你的問題並不那麼清楚。我從中得到的是你想顯示某些字段已經從某些模型中獲取的值填充。您既沒有展示模型的結構,也沒有提供更多的信息來獲得幫助。 –
你在說什麼名字和什麼ID? –