如何使用OneToOne Field保存表单向导

时间:2015-01-25 11:24:50

标签: django forms formwizard

Django 1.6 我有2个型号:

  1. 用户标准型号
  2. 资料
  3. 代码:

    class Profile(models.Model):
       class Meta:
           db_table = 'profile'
       user = models.OneToOneField(User)
       first_name = models.CharField(max_length=32)
       last_name = models.CharField(max_length=32)
       email = models.EmailField(max_length=32)
       bdate = models.DateField()
       phone = models.CharField(max_length=10)
    

    我使用表单向导注册用户。首先用户写入用户名和密码。接下来他写了个人资料字段如何将此信息保存到模型中。 我的观点:

    class UsrCreateProfile(SessionWizardView):
      template_name = 'user_registration.html'
      def done(self, form_list, **kwargs):
          form_list[0].save()
          get_user_name = form_list[0]
          get_user_profile = form_list[1]
          get_user_name = get_user_name['username']
          user_get = User.objects.get(username=get_user_name)
          form_list[1].save(pk=user_get)
          return HttpResponseRedirect('/')
    

    forms.py:

    class UserRegistrationForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'password1']
    class UserRegistrationForm1(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['first_name', 'last_name', 'email', 'bdate', 'phone']
    

    模板:

    {% block reg_usr %}
    <p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
    {% for field in form %}
        {{ field.error }}
    {% endfor %}
    <form method="post">{% csrf_token %}
        <table>
            {{ wizard.management_form }}
            {% if wizard.form.forms %}
                {{ wizard.form.management_form %}
                {% for form in wizard.form.forms %}
                    {{ form }}
                {% endfor %}
            {% else %}
                {{ wizard.form }}
            {% endif %}
            </table>
            {% if wizard.steps.prev %}
            <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">First step</button>
            <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">Prev step</button>
            {% endif %}
        <input type="submit" value="Next"/>
    </form>
    

    {%endblock%}

0 个答案:

没有答案