如何在基于Django类的视图中以单个html形式分配多个模型?

时间:2018-06-19 12:12:39

标签: python django django-forms django-views

问候,

我是Django的新手。我正在使用Django 2.0。 我需要在一个HTML表单中显示多个模型表单,然后一键保存。

form.py文件在此处

class BusinessProfileForm(forms.ModelForm):
  class Meta:
    model = StUserDetails
    fields = ('user_address1', 'user_address2', 'user_city', 'user_state', 'user_country','user_zip',)

class AdditionalDetailsForm(forms.ModelForm):
  class Meta:
    model = StUserAdditionalDetails
    fields = ('phone_number', 'fax_number', 'corporation_number', 'tax_number', 'custom_name1','cutom_field_value1', 'custom_name2','cutom_field_value2')

view.py

class UserProfileView(LoginRequiredMixin, FormView):
 template_name = 'users/add_user_details.html'
 success_url = '/dashboard/'
 form_class =
   [
      BusinessProfileForm,
      AdditionalDetailsForm,
 ]

 def form_valid(self, form):
     if form.is_valid():

         profile = form.save(commit=False)
         profile.user = self.request.user
         profile.save()

         messages.add_message(self.request, messages.SUCCESS, _('Profile data has been successfully updated.'))

         return super(UserProfileView, self).form_valid(form) 

和HTML呈现是-

 <form action="POST">
 <div>{{BusinessProfileForm}}(form1) <div>
 <div>{{AdditionalDetailsForm}}(form2) <div>
  <button type="submit">Save</button>
 </form>

请帮助我,我正在google上搜索,但始终无法理解。请不要使其重复。对于Django 2.0,我没有得到相同的问题

0 个答案:

没有答案