【问题标题】:Django User Profile Form EmptyDjango 用户配置文件表单空
【发布时间】:2013-02-05 00:08:23
【问题描述】:

我知道他们已经有很多关于此的帖子,但我尝试了很多解决方案,但我无法显示我的表单!

我想做一些非常简单的事情(我是 Django 初学者),我创建了一个特定的 UserProfile 来扩展基本的,我想让用户编辑它:

这是我的模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    cc = models.CharField(max_length=400, blank=True)
    lang = models.CharField(max_length=5, blank=True, )
    def __unicode__(self):
        return unicode(self.user.email)

class UserProfileForm(forms.ModelForm):
    class meta:
        model = UserProfile
    def __init__(self,*args,**kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)
        self.user = kwargs['instance']

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

这是我的查看方法:

@login_required
def accountform(request):
    data = {}
    data.update(csrf(request))
    user_profile = request.user.get_profile()
    data['form'] = UserProfileForm(instance=user_profile)
    print user_profile
    return render(request, 'accountform.html', data)

这是我的模板:

<form action="/contact/" method="post">
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>

当我显示 from 我只看到提交按钮...

【问题讨论】:

  • 一切正常,发帖时有没有漏掉什么?
  • 不,我没有错过任何东西,我现在被困在这上面......

标签: django django-forms django-users


【解决方案1】:

meta 内部类应该大写 - Meta

class UserProfileForm(forms.ModelForm):
    class Meta:  # not meta
        model = UserProfile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多