具有Instance = Session ID的ModelForm

时间:2015-02-10 05:46:37

标签: django django-forms django-views

我正在尝试为用户创建自定义表单,以便他们可以编辑自己的个人资料。

因此,当他们登录时,我会在会话中保存用户ID,并尝试使用内部的用户数据显示自定义表单(由3个类组成),但它不起作用...

forms.py:

class EditUser(forms.ModelForm):
    class Meta:
        model = User
        fields = ['first_name', 'last_name']

class EditUserProfilePublic(forms.ModelForm):
    class Meta:
        model = UserProfilePublic
        fields = ['website', 'facebook', 'twitter', 'google', 'books_frequency', 'favorite_types_of_books', 'hobbies', 'quote']

class EditUserProfilePrivate(forms.ModelForm):
    class Meta:
        model = UserProfilePrivate
        fields = ['address', 'address_bis', 'zipcode', 'city']

views.py:

@login_required
def edit_user(request):
    if request.method == 'POST':
        pass
    else:
        member = User.objects.get(pk=request.session['user_id'])
        formUser = EditUser(instance=member)
        formPublic = EditUserProfilePublic(instance=member)
        formPrivate = EditUserProfilePrivate(instance=member)

    return render(request, 'membres/edit_user.html', locals())

template.html

<div class="large-6 medium-6 small-12 columns">
    <div class="row collapse prefix-radius">
        <label for="{{ formPublic.website.id_for_label }}">Site internet</label>
        <div class="large-2 medium-2 small-2 columns">
            <span class="prefix"><i class="fa fa-globe"></i></span>
        </div>
        <div class="large-10 medium-10 small-10 columns">
            {{ formPublic.website }}
        </div>
   </div>
</div> (and so on)

models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    slug = models.SlugField(max_length=30)
    avatar = models.ImageField(null=True, blank=True, upload_to="avatars/")

    def __str__(self):
        return self.user

class UserProfilePublic(models.Model):
    FREQUENCY_CHOICES = (
        ('', ''),
        ('1', 'Moins de 2 romans'),
        ('2', 'Entre 2 et 5 romans'),
        ('3', 'Entre 6 et 10 romans'),
        ('4', 'Plus de 2 romans')
    )

    user = models.OneToOneField(User)
    website = models.URLField(blank=True)
    facebook = models.URLField(blank=True)
    twitter = models.URLField(blank=True)
    google = models.URLField(blank=True)
    hobbies = models.CharField(max_length=250, blank=True)
    books_frequency = models.CharField(max_length=1, choices=FREQUENCY_CHOICES, default='')
    favorite_types_of_books = models.TextField(blank=True)
    favorite_books = models.TextField(blank=True)
    quote = models.CharField(max_length=250, blank=True)

    def __str__(self):
        return self.user

class UserProfilePrivate(models.Model):
    user = models.OneToOneField(User)
    address = models.CharField(max_length=250, blank=True)
    address_bis = models.CharField(max_length=250, blank=True)
    zipcode = models.CharField(max_length=5, blank=True)
    city = models.CharField(max_length=30, blank=True)

    def __str__(self):
        return self.user

我的表单仍为空,没有显示任何错误... 顺便问一下,我可以使用管理工具来做这件事吗?

2 个答案:

答案 0 :(得分:0)

您的EditUserProfilePublic表单的实例应该是UserProfilePublic模型的实例,您正在传递User实例。像

这样的东西

formPublic = EditUserProfilePublic(instance = member.userprofilepublic)

与EditUserProfilePrivate表格相同。

-

答案 1 :(得分:0)

无需在会话中存储用户ID,也无需在视图中查询User表。用户ID已经在会话中,因为这只是登录的工作方式,因为登录request.user已经是相关的用户实例。

但是,您需要将实际的UserProfilePublic和UserProfilePrivate实例传递给相关表单,而不是User实例。所以:

@login_required
def edit_user(request):
    formUser = EditUser(instance=request.user)
    formPublic = EditUserProfilePublic(instance=request.user.userprofilepublic)
    formPrivate = EditUserProfilePrivate(instance=request.user.userprofileprivate)

这假设您的User类与UserProfilePublic和UserProfilePrivate具有一对一的关系,并且您没有更改related_name属性。